如何在解析csv文件时检查空行?

时间:2016-01-07 14:25:24

标签: java

从第三方日报中我将获得类似的csv文件,如下所示

07-Jan-2016
It is better to lead from behind and to put others in front, especially when you celebrate victory when nice things occur. You take the front line when there is danger. Then people will appreciate your leadership.

The main thing that you have to remember on this journey is, just be nice to everyone and always smile.

我的要求是我需要将每个段落(空格后一行)的每个引用放在一个单独的StringBuffer

中作为参考

我的问题如何检查空行?

我试过

if(line.contains("            "))
{
System.out.println("hjjjjjjjjjjjjjjjjk");
}

但上述问题导致存在空间

我正在阅读csv文件,如下所示

 String csvFile = "ip/ASRER070116.csv";
  BufferedReader br = null;
  String line = "";
  try {
   br = new BufferedReader(new FileReader(csvFile));
   while ((line = br.readLine()) != null) {
    if (line.startsWith(",")) {
     line = line.replaceFirst(",", "");
    }
    System.out.println(line);
   }
  } 

你能告诉我如何解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

if (line != null && (line.trim().equals("") || line.trim().equals("\n"))){
   System.out.println("this is empty line");
}

答案 1 :(得分:1)

我建议您在阅读行上使用trim(),然后检查line.length == 0是否

答案 2 :(得分:0)

您可以使用StringUtils.isBlank

中的Apache Commons Lang方法
  

public static boolean isBlank(CharSequence cs)

     

检查CharSequence是否为空格,为空(“”)或为空。

 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true
 StringUtils.isBlank(" ")       = true
 StringUtils.isBlank("bob")     = false
 StringUtils.isBlank("  bob  ") = false