2014-06-17 6 views
2

J'ai nombre de fichiers texte dans le format suivant:la suppression des espaces blancs supplémentaires à partir de fichiers texte

196903274115371008 @266093898 

Prince George takes his first public steps with his mom,        Catherine, Duchess of  

Cambridge. 

Je voudrais supprimer tous supplémentaire tandis que les espaces + nouveaux caractères en ligne, sauf les premiers caractères de nouvelle ligne. Je voudrais donc ci-dessus pour ressembler à ceci:

[email protected] 

Prince George takes his first public steps with his mom, Catherine, Duchess of Cambridge. 

j'ai écrit le code suivant:

package remove_white_space222; 

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 


public class Remove_white_space222 { 

    public static void main(String[] args) throws FileNotFoundException, IOException { 

     FileReader fr = new FileReader("input.txt"); 
     BufferedReader br = new BufferedReader(fr); 
     FileWriter fw = new FileWriter("outfile.txt"); 
     String line; 

     while((line = br.readLine()) != null) 
     { 
      line = line.trim(); // remove leading and trailing whitespace 
      line=line.replaceAll("\\s+", " "); 
      fw.write(line); 


     } 
     fr.close(); 
     fw.close(); 
    } 

} 

Merci d'avance pour votre aide ,,,,

+2

+1 pour le code propre et l'explication de ce que vous voulez .. Rarement vu sur SOF. – TheLostMind

Répondre

0

Voici une approche:

public static void main(String[] args) throws IOException { 
     FileReader fr = new FileReader("input.txt"); 
     BufferedReader br = new BufferedReader(fr); 
     FileWriter fw = new FileWriter("outfile.txt"); 
     String line; 

     int lineNum = 0; 
     while((line = br.readLine()) != null) 
     { 
      //check if we are working with the first two lines 
      //(which should remain untouched) 
      if (lineNum > 1) { 
       //make sure we ignore any empty lines 
       if (line.trim().length() > 0) { 
        //add a space to the end of each line to make 
        //padding before we append the next line. 
        line=line.trim().replaceAll("\\s+", " ") + " "; 
       } 
      } else { 
       //remove all whitespace. 
       line = line.trim().replaceAll("\\s", ""); 
       line = line + "\n"; 
      } 
      fw.write(line); 
      lineNum++; 
     } 
     fr.close(); 
     fw.close(); 
} 

sortie:

[email protected] 

Prince George takes his first public steps with his mom, Catherine, Duchess of Cambridge. % 
0

Vous pouvez utiliser l'état via une énumération pour ajouter des retours à la ligne après la première ligne et toutes les lignes vides qui la suivent.

package remove_white_space222; 

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.PrintWriter 
import java.io.IOException; 


public class Remove_white_space222 { 

    enum Status { 

     FIRST, EMPTY, NORMAL; 
    } 

    public static void main(String[] args) throws FileNotFoundException, IOException { 

     FileReader fr = new FileReader("input.txt"); 
     BufferedReader br = new BufferedReader(fr); 
     FileWriter fw = new FileWriter("outfile.txt"); 
     PrintWriter pw = new PrintWriter(fw); 
     String line; 

     while((line = br.readLine()) != null) 
     { 
      line = line.trim(); // remove leading and trailing whitespace 
      line=line.replaceAll("\\s+", " "); 
      fw.write(line); 
      if (status != Status.NORMAL) { 
       if ((status == Status.FIRST) || line.isEmpty()) { 
        pw.println(); 
        status = Status.EMPTY; 
       } else { 
        status = Status.NORMAL; 
       } 
      } 
     } 
     fr.close(); 
     fw.close(); 
    } 

} 
Questions connexes