我想通过从文本文件中分割|
包含空格并存储到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;
}
答案 0 :(得分:2)
试试吧。这意味着split string include space
。
String[] accounts = line.split(splitter, -1);
答案 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会为您带来下一行而不是您需要的数据
其次,它可能会导致您的异常,因为您正在检查下一行是否存在,并且您尝试读取两行,如果您只有一行,这将显然会失败
答案 2 :(得分:1)
固定代码:
帐户类:
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,所以你试图使用帐户[1],但帐户长度本身是1所以你将不得不处理如果account.length上的条款。你可以尝试上面的代码工作。从下次开始,我建议你在调试模式下运行你的代码来检查你在哪里获得异常。