【问题标题】:how to read an ArrayList from a text file in Java?如何从 Java 中的文本文件中读取 ArrayList?
【发布时间】:2010-05-09 21:09:48
【问题描述】:

我的ArrayList 的单个条目是这样的:

public class Account {
    String username;
    String password;
}

我设法将一些“帐户”放在一个文本文件中,但现在我不知道如何阅读它们。 这就是我的ArrayList 在文本文件中的样子:

用户名1 密码1 |用户名2 密码2 |等

这是我想出的代码的一部分,但它不起作用。

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

【问题讨论】:

  • 两天前问的问题几乎完全相同:stackoverflow.com/questions/2788080/reading-a-text-file-in-java您的情况有点复杂,但这些答案对您仍然有用。
  • 以后请多注意代码格式。将其重新格式化为可读文件所花费的时间超出了应有的时间。
  • 嗯,这似乎是某种续集:stackoverflow.com/questions/2777107/…
  • 事情并没有完全按照应有的方式进行,所以我不得不再次询问。很抱歉给您带来不便
  • @RichH:我没有做过这样的事情(无论如何是故意的)。我只是在编辑标签;页面结束了;不得不以某种方式修复它。请注意,SO 历史记录不会显示所有更改,并且编辑冲突比其他一些系统更成问题。

标签: java file-io arraylist


【解决方案1】:

一个更强大的解决方案是定义一个匹配该行的正则表达式并使用 Matcher.group(...) 调用来提取字段。例如,

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

在处理格式问题时,这也更加强大。它所做的一切都是寻找成对的单词。它不关心用什么字符串来分隔它们,也不关心它们之间有多少空格。

我只是猜到了正则表达式。您可能需要根据输入的确切格式对其进行一些调整。

【讨论】:

    【解决方案2】:

    不清楚您的问题有什么问题。但是,我希望您在包含循环内处理拆分“” (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);
                   }  
              }
    

    【讨论】:

    • 运行程序后,这是我得到的:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 1 在这一行:people.username = theword[i2];如果我再次运行它,它会在这一行显示:people.password = theword[i2+1];
    【解决方案3】:

    它做错了什么?您是否使用调试器逐步完成?如果是,是哪一部分导致了问题?

    我注意到的事情:

    1. 你的 i2 循环应该是 for (i2 = 0; i2 i2 += 2) {
    2. 除非您知道文件中有多少项,否则我不会设置 ArrayList 的初始大小。
    3. 您的用户名和密码之间是否有两个空格?
    4. 您是否研究过序列化?
    5. 为什么不为每个用户名和密码换行,这样加载会容易很多。

      用户名1
      密码1
      用户名2
      密码2

    【讨论】:

      猜你喜欢
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多