Java: Supprimer les lignes vides d’un Fichier

Author:

 int,string,char,static, url, socket, url, socket, java
{filelink=23}

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.FileWriter;




public class RemoveEmptyLine
 {
 	
  public static void main(String[] args) 
  {
  	try
  	{




    String fichier="c:/test.txt";
    InputStream fis = new FileInputStream(fichier);
    Reader reader = new InputStreamReader(fis, "utf-8");
    BufferedReader input =  new BufferedReader(reader);
    String line = null;
    StringBuilder str=new StringBuilder();
    while ((line = input.readLine()) != null)
    {
    	str.append(line);
    	str.append("\n");
  	}
  	writeTo(str.toString(), fichier);
  	}catch(IOException ex)
  	{
  		ex.printStackTrace();
  	}
      
  }
  private static void writeTo(String data, String fichier)throws IOException
  {
  	FileWriter writer=new FileWriter(fichier);
  	writer.write(data.replaceAll("(?m)^[ \t]*\r?\n", ""));
  	writer.close();
  }
  




}

Voir Ce Code dans Java7

[amazon_image id=”2746070553″ link=”true” target=”_blank” size=”medium” ]JAVA 7 – Les fondamentaux du langage Java[/amazon_image]

Leave a Reply

Your email address will not be published. Required fields are marked *