我想通过分割读取字符串值包括从文本文件到存储到Account类的空白.这是我读取文本文件的功能.
public ArrayList loadAccount(String fn) throws IOException{
ArrayList account = new ArrayList();
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 readFile() {
String file = "C:\Dev\JBoss\UpdateAccountNumber\source\AccountInformation.txt";
BufferedReader br = null;
String line = "";
String splitter = "\|";
List accountList = new ArrayList();
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
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;
}