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

如何在Java中从文本文件中读取ArrayList?,java,file-io,arraylist,Java,File Io,Arraylist,myArrayList的单个条目的格式如下: public class Account { String username; String password; } 我设法把一些“帐户”放在文本文件中,但现在我不知道如何读取它们。 这是我的ArrayList在文本文件中的外观: username1 password1 | username2 password2 | etc 用户名1密码1 |用户名2密码2 |等 这是我提出的代码的一部分,但它不起作用 public static

my
ArrayList
的单个条目的格式如下:

public class Account {
    String username;
    String password;
}
我设法把一些“帐户”放在文本文件中,但现在我不知道如何读取它们。 这是我的
ArrayList
在文本文件中的外观:

username1 password1 | username2 password2 | etc 用户名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");
    }
publicstaticvoidrdac(字符串参数[]){
ArrayList peoplelist=新的ArrayList(50);
inti,i2,i3;
字符串[]theword=null;
试一试{
FileReader fr=newFileReader(“myfile.txt”);
BufferedReader br=新的BufferedReader(fr);
字符串行=”;
而((line=br.readLine())!=null){
String[]theline=line.split(“|”);
对于(i=0;i对于(i3=0;i3您的问题不清楚出了什么问题。但是,我希望您在包含循环内处理对“”(
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);
               }  
          }
for(i=0;i
它做错了什么?您是否使用了调试器?如果是,是哪个部分导致了问题

我注意到的事情:

  • i2循环应该是for(i2=0;i2i2+=2){
  • 除非您知道文件中有多少项,否则我不会设置ArrayList的初始大小
  • 您的用户名和密码之间有两个空格吗
  • 你调查过序列化吗
  • 为什么不为每个用户名和密码设置一行新行呢?这样加载起来就容易多了

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


  • 一个更健壮的解决方案是定义一个与行匹配的正则表达式,并使用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 ...
    
    在处理格式问题时,它也更加健壮。它所做的只是寻找成对的单词。它不在乎用什么字符串来分隔它们,也不在乎它们之间有多少空格


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

    几乎与两天前提出的问题完全相同:您的案例稍微复杂一点,但这些答案仍然对您有用。在将来,请更加注意您的代码格式。这需要更多的时间韩:应该把它重新格式化成可读的东西。嗯,看起来像是这件事的续集:事情并没有按应有的方式发展,所以我不得不再次问。对不起,我的问题inconvenience@RichH当前位置我没有做这样的事(无论如何是故意的)。我只是在编辑标签;页面最终出错;不得不以某种方式进行修复。请注意,在这样的情况下,历史记录不会显示所有更改,编辑冲突的问题比其他一些系统更大。运行程序后,我得到的是:线程“main”java.lang.ArrayIndexOutofBounds中的异常异常:这一行中的异常:people.username=单词[i2];如果我再次运行它,它会在这行显示:people.password=theword[i2+1];