java 文件内容读取到字符串中,从文本文件中将字符串值读取到Java中的Jav...

我想通过分割读取字符串值包括从文本文件到存储到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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值