从文本文件中删除空行

时间:2014-10-29 09:57:52

标签: java

我设法让这段代码正常工作。它读取test.txt大约10000个单词(每个单词在它自己的行中)并首先将它们格式化为字母和第二个BY长度。然而,当我打开sort.txt时,我首先得到很多空行,然后这些单词被正确格式化。我的问题是如何删除空行,因为他们不能在那里。 .trim会工作吗?

package test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class sort {
public static class MyComparator implements Comparator<String>{
    @Override
    public int compare(String o1, String o2) {  
      if (o1.length() > o2.length()) {
         return 1;
      } else if (o1.length() < o2.length()) {
         return -1;
      }
      return o1.compareTo(o2);
    }
}
public static void main(String[] args) throws Exception {

    String inputFile = "test.txt";
    String outputFile = "sort.txt";

    FileReader fileReader = new FileReader(inputFile);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String inputLine;
    List<String> lineList = new ArrayList<String>();
    while ((inputLine = bufferedReader.readLine()) != null) {
        lineList.add(inputLine);
    }
    fileReader.close();

    Collections.sort(lineList,String.CASE_INSENSITIVE_ORDER);

    FileWriter fileWriter = new FileWriter(outputFile);
    PrintWriter out = new PrintWriter(fileWriter);
    for (String outputLine : lineList) {
        out.println(outputLine);
    }   

    Collections.sort(lineList, new MyComparator());

    FileWriter Fw = new FileWriter(outputFile);
    PrintWriter pw = new PrintWriter(fileWriter);
    for (String outputLine : lineList) {
        out.println(outputLine);
    }       
    out.flush();
    out.close();
    fileWriter.close();

}
}

4 个答案:

答案 0 :(得分:1)

不要添加那些空行:

while ((inputLine = bufferedReader.readLine()) != null) {
    if (!inputLine.isEmpty()) {
        lineList.add(inputLine);
    }
}

答案 1 :(得分:0)

你需要的只是

for (String outputLine : lineList) {
    if (!"".equals(outputLine.trim()))
        out.println(outputLine);
    //...

您不能单独使用.trim()来解决问题,因为它只会在开头和结尾处破坏空白。它将保持空String不变。我在这里使用它来确保你也省略了非空的行,但只有空格。

答案 2 :(得分:0)

当你实际上将字符串添加到列表中时,你可以处理它,这将是一个很好的方法,如

while (!StringUtils.isEmpty(inputLine = bufferedReader.readLine())) {

答案 3 :(得分:0)

管理以使其有效。这是解决方案。感谢您的所有回复。

package test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class sort {
public static class MyComparator implements Comparator<String>{
    @Override
    public int compare(String o1, String o2) {  
      if (o1.trim().length() > o2.trim().length()) {
         return 1;
      } else if (o1.trim().length() < o2.trim().length()) {
         return -1;
      }
      return o1.compareTo(o2);
    }
}
public static void main(String[] args) throws Exception {

    String inputFile = "test.txt";
    String outputFile = "sort.txt";

    FileReader fileReader = new FileReader(inputFile);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String inputLine;
    List<String> lineList = new ArrayList<String>();
    while ((inputLine = bufferedReader.readLine()) != null) {
        lineList.add(inputLine);
    }

    Collections.sort(lineList,String.CASE_INSENSITIVE_ORDER);
    Collections.sort(lineList, new MyComparator()); 

    FileWriter fileWriter = new FileWriter(outputFile);
    PrintWriter out = new PrintWriter(fileWriter);
    FileWriter Fw = new FileWriter(outputFile);
    PrintWriter pw = new PrintWriter(Fw);
    for (String outputLine : lineList) {
        if (!"".equals(outputLine.trim()))
            out.println(outputLine);        }       
    out.flush();
    out.close();
    fileWriter.close();

}
}