How do I read ArrayList from a text file in Java?

My only record ArrayList

is:

public class Account {
    String username;
    String password;
}

      

I was able to add some "accounts" to a text file, but now I don't know how to read them. This is what mine looks like ArrayList

in a text file:

username1 password1 | username2 password2 | etc

This is part of the code I came up with, but it doesn't work.

public static void RdAc(String args[]) {

    ArrayList<Account> peoplelist = new ArrayList<Account>(50);

    int i,i2,i3;
    String[] theword = null;

    try {

        FileReader fr = new FileReader("myfile.txt");
        BufferedReader br = new BufferedReader(fr);
        String line = "";

        while ((line = br.readLine()) != null) {
            String[] theline = line.split(" | "); 

            for (i = 0; i < theline.length; i++) {
                theword = theline[i].split("  "); 
            }

            for(i3=0;i3<theline.length;i3++)  { 
                Account people = new Account();

                for (i2 = 0; i2 < theword.length; i2++) {

                    people.username = theword[i2];
                    people.password = theword[i2+1];
                    peoplelist.add(people);
                }  
            } 

        }
    }
    catch (IOException ex) {
        System.out.println("Could not read from file");
    }

      

+2


a source to share


3 answers


a more reliable solution would be to define a regex to match the string and use a call to Matcher.group (...) to pull out the fields. eg,

String s = 
Pattern p = Pattern.compile("\\s*(\\w+)\\s+(\\w+)\\s+");
String line;
while ((line = br.readLine()) != null) {
  Matcher m = p.match(line);
  while (m.find()) {
    String uname = m.group(1);
    String pw = m.group(2);
... etc ...

      



it is also a much more reliable solution to format problems. all this looking for a couple of words. it doesn't care which line is used to separate them or the amount of space between them.

I just guessed in regex. you will probably need to tweak it a bit depending on the exact input format.

+3


a source


It is not clear what is wrong with your question. However, I expect you to process the splitting to "" ( theword

) results inside the containing loop, rather than process it outside.



      for (i = 0; i < theline.length; i++) {
               theword = theline[i].split("  "); 
               Account people = new Account();
               for (i2 = 0; i2 < theword.length; i2++) {
                    people.username = theword[i2];
                    people.password = theword[i2+1];
                    peoplelist.add(people);
               }  
          }

      

+2


a source


What is he doing wrong? Have you gone through the debugger? If so, what part of the causal problem?

What I noticed:

  • You must have an i2 loop for (i2 = 0; i2 <theword.length; i2 + = 2 ) {
  • I would not set the initial size of the ArrayList unless you know how many elements are in the file.
  • Are there two spaces between your username and password?
  • Have you studied serialization?
  • Why not create a new line for each username and password. It would be much easier to download.

    username1
     password1
     username2
     password2

+1


a source







All Articles