Rok Ivartnik
Rok Ivartnik

Reputation: 149

removing empty lines from txt file

I manage to get this code working. It reads test.txt with about 10000 words (each word in its own line) and formats them first Alphabeticly and second BY length. However when i open sort.txt i get first like a lot of empty lines after that the words are properly formated. My question is how to remove thos empty lines since they cant be there. Will .trim work?

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();

}
}

Upvotes: 0

Views: 3549

Answers (4)

Rok Ivartnik
Rok Ivartnik

Reputation: 149

Manage to make it work. Here is the solution. Thanks for all yours replies.

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();

}
}

Upvotes: 0

DeepInJava
DeepInJava

Reputation: 1961

you can handle it when you are actually adding string to list that would be good approach like

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

Upvotes: 0

chiastic-security
chiastic-security

Reputation: 20520

All you need is

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

You can't use .trim() on its own to solve the problem because it just hacks off whitespace at the beginning and end. It'll leave an empty String unchanged. I've used it here to make sure that you also omit lines that aren't empty but do have just whitespace in.

Upvotes: 0

Terry Storm
Terry Storm

Reputation: 497

just dont add those empty lines:

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

Upvotes: 1

Related Questions