【发布时间】:2011-10-04 22:35:05
【问题描述】:
我需要读取一个管道分隔文件并填充它 在 String 数组中并将其作为列表传递回来并进行进一步处理 下面。
Sample data : X|ABCD|001111006|1111006|ABC|test006| | | | | |C|||||||||||||
Note : Sample data can have both NULL and space between pipe
如果数据在所有管道分隔符中都可用,那么这工作正常,至少一个空格, 但是对于管道之间的空值
有 26 个属性,只要有 NULL,数组索引就不是 从 NULL 可用的基数递增。假设当第 12 个管道有 NULL 时,数组直到 25 才被填充,它在 12 停止,这是我进一步处理中的问题
并且文件可以同时包含 NULL 和空格。你能帮我解决这个问题吗
public List<String[]> readFile(String FileName) {
final List<String[]> userList = new ArrayList<String[]>();
BufferedReader bufferedreader = null;
try {
int i=0;
bufferedreader = new BufferedReader(new FileReader(FileName));
String line = null;
while ((line = bufferedreader.readLine()) != null) {
final String[] values = line.split("\\|");
userList.add(values);
}
}
catch (FileNotFoundException ex) {
CTLoggerUtil.logError(ex.getMessage());
}
catch (IOException ex) {
CTLoggerUtil.logError(ex.getMessage());
}
finally {
try {
if (bufferedreader != null)
{
bufferedreader.close();}
}
catch (IOException ex) {
CTLoggerUtil.logError(ex.getMessage());
}
}
return userList;
}
【问题讨论】:
-
String.split() 的文档说连续的分隔符不被视为一个。你能检查一下拆分后的 values.length 是什么吗?