将文本文件中的字符串值读取到 Java 中的 java arraylist

Reading string value from a text file to java arraylist in Java

我想通过从文本文件中拆分 | 包含白色 space 并存储到帐户来读取字符串值 class.This 是我读取文本文件的功能。

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

这是帐户 class。

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

这是AccountInformation.txt。

帐号 |遗留密钥 |说明

80000001|7001111|

80000002| |

80000003|7001234|测试

更新:这是我的 readFile class.Now,ok.I正在使用 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;
  }

如果我正确理解你的问题,你需要改变你的循环

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 会为您带来下一行,而不是您需要的数据

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

固定码:

帐号class:

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

测试class:

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,因此您正在尝试使用帐户 [1],但帐户长度本身为 1,因此您将不得不处理在 accounts.length 上带有 if 子句。你可以尝试上面的代码它的工作。从下次开始,我会建议您 运行 您的代码处于调试模式,以检查您在哪里遇到异常。

试试看。这意味着 split string include space .

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