commons-file/src/main/java/info/monitorenter/util/FileUtil.java
public static void removeDuplicateLineBreaks(final File f) {
    final String sep = StringUtil.getNewLine();
    if (!f.exists()) {
      System.err.println("FileUtil.removeDuplicateLineBreak(File f): " + f.getAbsolutePath()
          + " does not exist!");
    } else {
      if (f.isDirectory()) {
        System.err.println("FileUtil.removeDuplicateLineBreak(File f): " + f.getAbsolutePath()
            + " is a directory!");
      } else {
        // real file
        FileInputStream inStream = null;
        BufferedInputStream in = null;
        FileWriter out = null;
        try {
          inStream = new FileInputStream(f);
          in = new BufferedInputStream(inStream, 1024);
          StringBuffer result = new StringBuffer();
          int tmpread;
          while ((tmpread = in.read()) != -1) {
            result.append((char) tmpread);
          }
          String tmpstring;
          final StringTokenizer toke = new StringTokenizer(result.toString(), sep, true);
          result = new StringBuffer();
          int breaks = 0;
          while (toke.hasMoreTokens()) {
            tmpstring = toke.nextToken().trim();
            if (tmpstring.equals("") && (breaks > 0)) {
              breaks++;
              // if(breaks<=2)result.append(sep);
              continue;
            }
            if (tmpstring.equals("")) {
              tmpstring = sep;
              breaks++;
            } else {
              breaks = 0;
            }
            result.append(tmpstring);
          }
          // delete original file and write it new from tmpfile.
          f.delete();
          f.createNewFile();
          out = new FileWriter(f);
          out.write(result.toString());
        } catch (final FileNotFoundException e) {
          // does never happen.
        } catch (final IOException g) {
          g.printStackTrace(System.err);
        } finally {
          if (in != null) {
            try {
              in.close();
            } catch (final IOException e) {
              e.printStackTrace();
            }
          }
          if (out != null) {
            try {
              out.flush();
              out.close();
            } catch (final IOException e) {
              e.printStackTrace();
            }
          }
        }
      }
    }
  }
public static void removeDuplicateLineBreaks(final File f) {
    final String sep = StringUtil.getNewLine();
    if (!f.exists()) {
      System.err.println("FileUtil.removeDuplicateLineBreak(File f): " + f.getAbsolutePath()
          + " does not exist!");
    } else {
      if (f.isDirectory()) {
        System.err.println("FileUtil.removeDuplicateLineBreak(File f): " + f.getAbsolutePath()
            + " is a directory!");
      } else {
        // real file
        FileInputStream inStream = null;
        BufferedInputStream in = null;
        FileWriter out = null;
        try {
          inStream = new FileInputStream(f);
          in = new BufferedInputStream(inStream, 1024);
          StringBuffer result = new StringBuffer();
          int tmpread;
          while ((tmpread = in.read()) != -1) {
            result.append((char) tmpread);
          }
          String tmpstring;
          final StringTokenizer toke = new StringTokenizer(result.toString(), sep, true);
          result = new StringBuffer();
          int breaks = 0;
          while (toke.hasMoreTokens()) {
            tmpstring = toke.nextToken().trim();
            if (tmpstring.equals("") && (breaks > 0)) {
              breaks++;
              // if(breaks<=2)result.append(sep);
              continue;
            }
            if (tmpstring.equals("")) {
              tmpstring = sep;
              breaks++;
            } else {
              breaks = 0;
            }
            result.append(tmpstring);
          }
          // delete original file and write it new from tmpfile.
          f.delete();
          f.createNewFile();
          out = new FileWriter(f);
          out.write(result.toString());
        } catch (final FileNotFoundException e) {
          // does never happen.
        } catch (final IOException g) {
          g.printStackTrace(System.err);
        } finally {
          if (in != null) {
            try {
              in.close();
            } catch (final IOException e) {
              e.printStackTrace();
            }
          }
          if (out != null) {
            try {
              out.flush();
              out.close();
            } catch (final IOException e) {
              e.printStackTrace();
            }
          }
        }
      }
    }
  }
public static void removeDuplicateLineBreaks ( final File f ) { final String sep = StringUtil . getNewLine ( ) ; if ( ! f . exists ( ) ) { System . err . println ( "FileUtil.removeDuplicateLineBreak(File f): " + f . getAbsolutePath ( ) + " does not exist!" ) ; } else { if ( f . isDirectory ( ) ) { System . err . println ( "FileUtil.removeDuplicateLineBreak(File f): " + f . getAbsolutePath ( ) + " is a directory!" ) ; } else { // real file FileInputStream inStream = null ; BufferedInputStream in = null ; FileWriter out = null ; try { inStream = new FileInputStream ( f ) ; in = new BufferedInputStream ( inStream , 1024 ) ; StringBuffer result = new StringBuffer ( ) ; int tmpread ; while ( ( tmpread = in . read ( ) ) != - 1 ) { result . append ( ( char ) tmpread ) ; } String tmpstring ; final StringTokenizer toke = new StringTokenizer ( result . toString ( ) , sep , true ) ; result = new StringBuffer ( ) ; int breaks = 0 ; while ( toke . hasMoreTokens ( ) ) { tmpstring = toke . nextToken ( ) . trim ( ) ; if ( tmpstring . equals ( "" ) && ( breaks > 0 ) ) { breaks ++ ; // if(breaks<=2)result.append(sep); continue ; } if ( tmpstring . equals ( "" ) ) { tmpstring = sep ; breaks ++ ; } else { breaks = 0 ; } result . append ( tmpstring ) ; } // delete original file and write it new from tmpfile. f . delete ( ) ; f . createNewFile ( ) ; out = new FileWriter ( f ) ; out . write ( result . toString ( ) ) ; } catch ( final FileNotFoundException e ) { // does never happen. } catch ( final IOException g ) { g . printStackTrace ( System . err ) ; } finally { if ( in != null ) { try { in . close ( ) ; } catch ( final IOException e ) { e . printStackTrace ( ) ; } } if ( out != null ) { try { out . flush ( ) ; out . close ( ) ; } catch ( final IOException e ) { e . printStackTrace ( ) ; } } } } } }
Removes the duplicate line breaks in the given file.
<p>

Be careful with big files: In order to avoid having to write a tmpfile
(cannot read and directly write to the same file) a StringBuffer is used
for manipulation. Big files will cost all RAM and terminate VM hard.
<p>

@param f
the file to remove duplicate line breaks in.
Removes the duplicate line breaks in the given file . <p >