简体   繁体   English

如何从文件读取到数组列表

[英]How to read from a file to an arraylist

i'm having some trouble with reading from a text file back and writing back to an array list. 我在从文本文件读回并写回数组列表时遇到了一些麻烦。 I was wondering if you could tell me where I'm going wrong? 我想知道你能否告诉我我要去哪里错了? Thanks. 谢谢。

accountArrayList = new ArrayList<BankAccount>();

private void fileIn()
{
    try
    {
        createTestAccounts();
        //Scanner input = new Scanner(new FileReader(bankFile));
        JOptionPane.showMessageDialog(null, "File: " + bankFile + " has been opened for importing");
        BufferedReader reader = new BufferedReader(new FileReader("bankFile.txt"));
        String account = reader.readLine();
        while(account !=null) {
           accountArrayList.add(account); // - add a new account to the text file, but exception show that String cannot be converted to Bank Account
           account = reader.readLine();
        }
        reader.close();
    }
    catch(Exception ex)
    {
        JOptionPane.showMessageDialog(null, "not found");
    }
}

You are adding a String , but the list.add method expects an object of type BankAccount . 您正在添加一个String ,但是list.add方法需要一个BankAccount类型的对象

You'll have to find a way to turn that String into an Object of that type , then add the latter. 您将必须找到一种将String转换为该类型的Object的方法,然后添加后者。 Maybe there is a fromString() factory method? 也许有一个fromString()工厂方法? Or a Constructor that takes an initialization - String ? 还是需要初始化的构造方法-String?

If there is a constructor then it should look like 如果有一个构造函数,那么它应该看起来像

accountArrayList.add(new BankAccount(account)); 

To read all lines you may use (if your file is in UTF-8): 要读取所有行,可以使用(如果文件为UTF-8):

List<String> allLines = Files.readAllLines("bankFile.txt", StandardCharsets.UTF_8);

But as it was mentioned in the other comments you will need to transform a String into a BankAccount 但是,正如其他注释中提到的那样,您需要将String转换为BankAccount

BankAccount ba = new BankAccount(account);
accountArrayList.add(ba);

You're going to need something like this...depending on what your BankAccount class is exactly. 您将需要类似这样的东西...具体取决于您的BankAccount类是什么。

The arraylist expects an BankAccount object, instead you are reading from a file, so the arraylist should be String. arraylist需要一个BankAccount对象,而不是从文件中读取,因此arraylist应该为String。

ArrayList<String> accountArrayList = new ArrayList<>();

For the while loop condition, personally, i always use, but you can do it like that too. 对于while循环条件,我个人总是使用,但是您也可以这样做。

while((account = reader.readLine()) != null){
    accountArrayList.add(account);
}

Assuming you have a String constructor for BankAccount , something like this: 假设您有一个BankAccountString构造函数,如下所示:

public BankAccount(String account) {
    this.account = account;
}

You can use Apache commons-io and java 8 to get a one-liner! 您可以使用Apache commons-io和Java 8获得单行代码!

List<BankAccount> accounts = FileUtils.readLines(new File(filename))
    .stream().map(BankAccount::new).collect(Collectors.toList());

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

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