我有一个BufferedReader遍历一个CSV文件的行;当它到达文件的末尾,则返回以下错误:为什么这个BufferedReader返回空字符串而不是null?
Exception in thread "main" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:992)
我如何去实现它到达文件的末尾,输入为空,而不是空的读者?我检查过这个文件,最后一行末尾没有空格。
代码:
File filReadMe = new File(inputFile);
BufferedReader brReadMe = new BufferedReader(new InputStreamReader(new FileInputStream(filReadMe), "UTF-8"));
try
{
String strLine;
while ((strLine = brReadMe.readLine()) != null)
{
System.out.println(strLine);
//place the line into CsvRecordFactory
int record = csv.processLine(strLine, input);
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
//Close the BufferedReader
try {
if (brReadMe != null)
brReadMe.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
你文件的最后一行包含一个空的'String',所以你应该检查'strLine.equals(“”)'是否是停止'while循环'的另一个条件。 –
strLine.isEmpty()在语义上比strLine.equals更好(“”) –
感谢Luiggi和Cyrille。你知道我的语法有什么问题吗?我仍然看到同样的错误。 (strLine = brReadMe.readLine())!= null &&!strLine.isEmpty()) – user2040880