读一个管道分隔的文件时,Java的Null值引起的问题 [英] Java Null value causing issue when reading a pipe delimited file
问题描述
我有一个要求读一个管道分隔的文件并将其填充
在一个String数组,并把它作为一个列表,并做进一步的处理
下文。
的样本数据:X | ABCD | 001111006 | 1111006 | ABC | test006 | | | | | | C |||||||||||||
注:样本数据可以同时拥有空管之间的空间
这是工作的罚款,如果数据是在所有的管道区划界定可用,ATLEAST空间,
但在管之间的空值
有26属性和每当有空数组索引不是
从基本空的地方是可利用的增加。说的时候有12空管第阵列未填充,直到25停靠在12,在我进一步的处理这craeting问题
和该文件可以同时具有NULL和空间。你能不能帮我解决这个问题。
公开名单<的String []> READFILE(字符串文件名){最终名单<的String []>用户列表=新的ArrayList<的String []>();
的BufferedReader的BufferedReader = NULL;
尝试{ INT I = 0;
的BufferedReader =新的BufferedReader(新的FileReader(文件名));
串线= NULL; 而((行= bufferedreader.readLine())!= NULL){
最终的String []值= line.split(\\\\ |);
userList.add(值);
}
}
赶上(FileNotFoundException异常前){
CTLoggerUtil.logError(ex.getMessage());
}
赶上(IOException异常前){
CTLoggerUtil.logError(ex.getMessage());
}
最后{
尝试{
如果(的BufferedReader!= NULL)
{
bufferedreader.close();}
}
赶上(IOException异常前){
CTLoggerUtil.logError(ex.getMessage());
}
} 返回用户列表;
}
默认情况下, String.split()
丢弃空尾随字符串。如果设置了负的限制,您将得到所有空的结果反馈,以及:
最终的String []值= line.split(\\\\ |,-1);
I have a requirement to read a pipe delimited file and have it populated in a String array and pass it as a list back and do further processing below.
Sample data : X|ABCD|001111006|1111006|ABC|test006| | | | | |C|||||||||||||
Note : Sample data can have both NULL and space between pipe
This is working fine if data is available in all the pipe delimits , atleast a space, but for the null values in between the pipe
There are 26 attributes and whenever there are NULL the array index is not incremented from the cardinal where NULL was available . Say when there is a NULL at 12 th pipe the array is not populated till 25 it stops at 12 and this is craeting issue in my further processing
and the file can have both NULL and space. Can you help me solve this issue
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;
}
By default, String.split()
discards empty trailing strings. If you set a negative limit, you will get all empty results back as well:
final String[] values = line.split("\\|", -1);
这篇关于读一个管道分隔的文件时,Java的Null值引起的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!