Java 忽略 CSV 文件中的空行
Ignoring blank lines in CSV file in Java
我正在尝试遍历 Java 中的 CSV 文件。它遍历整个文件,但会到达文件末尾并尝试读取下一个空行并抛出错误。我的代码如下。
public class Loop() {
public static void main(String[] args) {
BufferedReader br = null;
String line = "";
try {
HashMap<Integer, Integer> changeData = new HashMap<Integer, Integer>();
br = new BufferedReader(new FileReader("C:\xxxxx\xxxxx\xxxxx\the_file.csv"));
String headerLine = br.readLine();
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
/*Below is my latest attempt at fixing this,*/
/*but I've tried other things too.*/
if (data[0].equals("")) { break; }
System.out.println(data[0] + " - " + data[6]);
int changeId = Integer.parseInt(data[0]);
int changeCv = Integer.parseInt(data[6]);
changeData.put(changeId, changeCv);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
就像我输入的那样,这在到达文件末尾之前工作正常。当它到达文件末尾时,我收到错误 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at com.ucg.layout.ShelfTableUpdates.main(ShelfTableUpdates.java:23)
。我通过在 Spring Tool Suite 中调试它逐步完成了代码。每当我尝试引用 data[0] 或 data[6] 时,就会出现错误;可能是因为该行中没有任何内容。这让我回到了我最初的问题,即为什么它甚至首先尝试阅读该行。
我的理解是 while ((line = br.readLine()) != null)
会检测到文件的结尾,但它似乎不是。我试过重新打开文件并删除所有空白行,但这没有用。
知道如何检测文件的结尾以便我不会在这段代码中收到错误吗?
答案:
感谢用户@quemeraisc。我还能够用空格替换逗号,如果该行等于 null 或“”,那么您就知道它是文件的末尾;在我的例子中,文件末尾之前没有空白行。这仍然没有解决检测文件末尾的问题,因为如果我的数据之间确实有不是 EOF 的空白行,那么这将检测到那些。
解决方案一:
if (data.length < 7) {
System.out.println(data.length);
break;
}
解决方案一:
if (line.replace(",", "").equals(null) || line.replace(",", "").equals("")) {
System.out.println(line.replace(",", ""));
break;
}
跳过所有空行:
while ((line = br.readLine()) != null) {
if( line.trim().isEmpty() ) {
continue;
}
....
....
最后一行可能包含一些控制字符(如换行符、回车符 return、EOF 和其他不可见字符),在这种情况下,简单的 String#trim() 不会删除他们,请参阅此答案以了解如何删除它们:How can i remove all control characters from a java string?
要跳过空白行,您可以尝试:
while ((line = reader.readLine()) != null) {
if(line.length() > 0) {
String[] data = line.split(",");
/*Below is my latest attempt at fixing this,*/
/*but I've tried other things too.*/
if (data[0] == null || data[0].equals("")) { break; }
System.out.println(data[0] + " - " + data[6]);
int changeId = Integer.parseInt(data[0]);
int changeCv = Integer.parseInt(data[6]);
changeData.put(changeId, changeCv);
}
}
public String readLine()
将从您的文件中读取一行,甚至是空行。因此,当您拆分行时,如 String[] data = line.split(",");
中那样,您会得到一个大小为 1 的数组。
为什么不试试 :
if (data.length >= 7)
{
System.out.println(data[0] + " - " + data[6]);
int changeId = Integer.parseInt(data[0]);
int changeCv = Integer.parseInt(data[6]);
changeData.put(changeId, changeCv);
}
这将确保您的数组中至少有 7 个元素,然后再继续。
使用 replaceAll 方法代替 replace 方法。然后就可以了。
我正在尝试遍历 Java 中的 CSV 文件。它遍历整个文件,但会到达文件末尾并尝试读取下一个空行并抛出错误。我的代码如下。
public class Loop() {
public static void main(String[] args) {
BufferedReader br = null;
String line = "";
try {
HashMap<Integer, Integer> changeData = new HashMap<Integer, Integer>();
br = new BufferedReader(new FileReader("C:\xxxxx\xxxxx\xxxxx\the_file.csv"));
String headerLine = br.readLine();
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
/*Below is my latest attempt at fixing this,*/
/*but I've tried other things too.*/
if (data[0].equals("")) { break; }
System.out.println(data[0] + " - " + data[6]);
int changeId = Integer.parseInt(data[0]);
int changeCv = Integer.parseInt(data[6]);
changeData.put(changeId, changeCv);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
就像我输入的那样,这在到达文件末尾之前工作正常。当它到达文件末尾时,我收到错误 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at com.ucg.layout.ShelfTableUpdates.main(ShelfTableUpdates.java:23)
。我通过在 Spring Tool Suite 中调试它逐步完成了代码。每当我尝试引用 data[0] 或 data[6] 时,就会出现错误;可能是因为该行中没有任何内容。这让我回到了我最初的问题,即为什么它甚至首先尝试阅读该行。
我的理解是 while ((line = br.readLine()) != null)
会检测到文件的结尾,但它似乎不是。我试过重新打开文件并删除所有空白行,但这没有用。
知道如何检测文件的结尾以便我不会在这段代码中收到错误吗?
答案: 感谢用户@quemeraisc。我还能够用空格替换逗号,如果该行等于 null 或“”,那么您就知道它是文件的末尾;在我的例子中,文件末尾之前没有空白行。这仍然没有解决检测文件末尾的问题,因为如果我的数据之间确实有不是 EOF 的空白行,那么这将检测到那些。
解决方案一:
if (data.length < 7) {
System.out.println(data.length);
break;
}
解决方案一:
if (line.replace(",", "").equals(null) || line.replace(",", "").equals("")) {
System.out.println(line.replace(",", ""));
break;
}
跳过所有空行:
while ((line = br.readLine()) != null) {
if( line.trim().isEmpty() ) {
continue;
}
....
....
最后一行可能包含一些控制字符(如换行符、回车符 return、EOF 和其他不可见字符),在这种情况下,简单的 String#trim() 不会删除他们,请参阅此答案以了解如何删除它们:How can i remove all control characters from a java string?
要跳过空白行,您可以尝试:
while ((line = reader.readLine()) != null) {
if(line.length() > 0) {
String[] data = line.split(",");
/*Below is my latest attempt at fixing this,*/
/*but I've tried other things too.*/
if (data[0] == null || data[0].equals("")) { break; }
System.out.println(data[0] + " - " + data[6]);
int changeId = Integer.parseInt(data[0]);
int changeCv = Integer.parseInt(data[6]);
changeData.put(changeId, changeCv);
}
}
public String readLine()
将从您的文件中读取一行,甚至是空行。因此,当您拆分行时,如 String[] data = line.split(",");
中那样,您会得到一个大小为 1 的数组。
为什么不试试 :
if (data.length >= 7)
{
System.out.println(data[0] + " - " + data[6]);
int changeId = Integer.parseInt(data[0]);
int changeCv = Integer.parseInt(data[6]);
changeData.put(changeId, changeCv);
}
这将确保您的数组中至少有 7 个元素,然后再继续。
使用 replaceAll 方法代替 replace 方法。然后就可以了。