简体   繁体   English

如何从Java中的文本文件读取ArrayList?

[英]how to read an ArrayList from a text file in Java?

The single entry of my ArrayList is of the form: 我的ArrayList的单个条目具有以下形式:

public class Account {
    String username;
    String password;
}

I managed to put some "Accounts" in the a text file, but now I don't know how to read them. 我设法在文本文件中添加了一些“帐户”,但是现在我不知道如何阅读它们。 This is how my ArrayList looks in the text file: 这是我的ArrayList在文本文件中的外观:

username1 password1 | username2 password2 | etc

This is a 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");
    }

a more robust solution would be to define a regular expression that matches the line and use the Matcher.group(...) call to pull out the fields. 一个更可靠的解决方案是定义一个与行匹配的正则表达式,并使用Matcher.group(...)调用提取字段。 for example, 例如,

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 ...

this is also a lot more robust when it comes to dealing with format problems. 在处理格式问题时,这也更加强大。 all it does it look for pairs of words. 它所做的一切都是寻找成对的单词。 it doesn't care what string is used to separate them or how many spaces there are in between. 不在乎使用什么字符串来分隔它们或它们之间有多少空格。

i just guessed at the regex. 我只是在正则表达式上猜到了。 you will probably need to tune it a bit depending on the exact format of the input. 您可能需要根据输入的确切格式对其进行调整。

It's not clear what's wrong from your question. 目前尚不清楚您的问题出了什么问题。 However, I'd expect you to process the results of splitting on " " ( theword ) within the containing loop, rather than processing it outside. 但是,我希望您处理在包含循环内对“”( theword )进行拆分的结果,而不是在外部进行处理。

      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);
               }  
          }

What does it do wrong? 它做错了什么? Have you stepped through with a debugger? 您是否已调试过调试器? If so, which part is causuing the issue? 如果是这样,那是造成该问题的原因?

Things that I've noticed: 我注意到的事情:

  1. You i2 loop should be for (i2 = 0; i2 < theword.length; i2 += 2 ) { 您的i2循环应该是(i2 = 0; i2 <theword.length; i2 + = 2 ){
  2. I wouldn't set the initial size of the ArrayList unless you know how many items are in the file. 除非您知道文件中有多少个项目,否则我不会设置ArrayList的初始大小。
  3. Are there two spaces between your username and password? 用户名和密码之间是否有两个空格?
  4. Have you looked into serialization? 您是否研究过序列化?
  5. Why not have a new line for each username and password It would be a lot easier to load. 为什么不为每个用户名和密码都添加一个新行呢?加载起来会容易得多。

    username1 USERNAME1
    password1 密码1
    username2 USERNAME2
    password2 密码2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM