在Java中将文本文件中的字符串值读取到Java ArrayList中

3
我想通过分割包括空格的文本文件中的|来读取字符串值,并将其存储到Account类中。这是我的读取文本文件的函数:
public ArrayList<Account> loadAccount(String fn) throws IOException{     
    ArrayList<Account> account = new ArrayList<Account>();
    Scanner infile = new Scanner(new InputStreamReader (new FileInputStream(fn)));
    while(infile.hasNextLine()){
        String accountNo = infile.nextLine();
        String legencyNo = infile.nextLine();
        Account c = new Account(accountNo, legencyNo);
        account.add(c);
    }
    infile.close();
    return account;
}     

这是一个账户类。

public class Account {
 private int id;
 private String accountNo;
 private String legencyNo;
}

这是AccountInformation.txt文件。

账户编号 | 传统密钥 | 描述

80000001|7001111|

80000002| |

80000003|7001234|测试

更新:这是我的readFile类。现在它可以正常使用了。我正在使用StringUtils。

public static List<Account> readFile() {

    String file = "C:\\Dev\\JBoss\\UpdateAccountNumber\\source\\AccountInformation.txt";
    BufferedReader br = null;
    String line = "";
    String splitter = "\\|";
    List<Account> accountList = new ArrayList<Account>();
    try {
        br = new BufferedReader(new FileReader(file));
        while ((line = br.readLine()) != null) {
            String[] accounts = org.apache.commons.lang3.StringUtils.split(line, splitter);
            String accountNo = "",legencyNo="";
            for(int i = 0;i<accounts.length;i++){
                if (i == 0){
                     accountNo = (accounts[0] == null) ? "" : accounts[0];
                }
                if (i==1){
                     legencyNo = (accounts[1] == null) ? "" : accounts[1];
                }
            }
            Account a = new Account(accountNo,legencyNo);
            accountList.add(a); 
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return accountList;
  }

1
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Aiken
@Aiken,我想读取上述文本文件并转换为Account对象。这就是我想要的。 - Kyaw Bo
3个回答

2

尝试使用as。这意味着 分割包含空格的字符串

String[] accounts = line.split(splitter, -1);

1
如果我理解你的问题正确,你需要改变你的循环。
while(infile.hasNextLine()){
            String line= infile.nextLine();
            String[] tokens = line.split("\\|");
            Account c = new Account(tokens[0], tokens[1]);
            account.add(c);
        }

有两个原因造成这种情况,

首先,你需要的所有数据都在单行中80000001 | 7001111 |,所以调用nextLine将带给你下一行而非你需要的数据。

其次,它可能会导致异常,因为你正在检查下一行是否存在,然后尝试读取两行,如果只有一行,显然会失败。


当我改变了循环,结果就像这样。 8 8 8 - Kyaw Bo
@KyawBo 抱歉,我的错,我已经修复了。显然,你需要跳过第一行,因为它包含了标题,但这是另外一个故事了。 - user902383

1
固定代码:

Fixed code:

账户类:

class Account
{
    private int id;
    private String accountNo;
    private String legencyNo;

    public Account(String accountNo, String legencyNo)
    {
        this.accountNo = accountNo;
        this.legencyNo = legencyNo;
    }

    @Override
    public String toString()
    {
        return this.accountNo + " " + this.legencyNo;
    }
}

测试类:

public class Test
{
    public static List<Account> readFile()
    {
        String file = "C:\\workspace\\practise\\test.txt";
        BufferedReader br = null;
        String line = "";
        String splitter = "\\|";
        List<Account> accountList = new ArrayList<Account>();
        try
        {

            br = new BufferedReader(new FileReader(file));
            while ((line = br.readLine()) != null)
            {
                Account a = null;
                String[] accounts = line.split(splitter);
                if (accounts.length > 1)
                {
                    a = new Account(accounts[0], accounts[1]);
                } else
                {
                    a=new Account(accounts[0], "");
                }
                accountList.add(a);
            }

        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            if (br != null)
            {
                try
                {
                    br.close();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

        return accountList;
    }

    public static void main(String[] args)
    {
        List<Account> acct = readFile();
        for (Account account : acct)
        {
            System.out.println(account);
        }
    }
}

测试文件:

Account Number|Legacy Key|Description
80000001|7001111|
80000002||
80000003|7001234|Testing

上面代码中的问题是,当您在第二条记录中拆分时,只有一个字符串存在,即80000002,因此您正在尝试使用accounts [1],但是accounts长度本身为1,因此您将不得不使用if子句处理accounts.length。您可以尝试上面的代码,它能正常工作。下次我建议您在调试模式下运行代码以检查异常出现的位置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接