title: removing empty lines from txt file
body: <p>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?</p>
  
  <pre><code>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&lt;String&gt;{
      @Override
      public int compare(String o1, String o2) {  
        if (o1.length() &gt; o2.length()) {
           return 1;
        } else if (o1.length() &lt; 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&lt;String&gt; lineList = new ArrayList&lt;String&gt;();
      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();
  
  }
  }
  </code></pre>