2016-11-19 51 views
0

我正在將PDF文件轉換爲文本並刪除有頁碼的行,但問題是它留下了2行的空白空間。所以我想刪除這些空間有2個或多個空行連續但如果1號線是empty.my代碼:如何刪除java中的文件中的空行

// Open the file 
     FileInputStream fstream = new FileInputStream("C:\\Users\\Vivek\\Desktop\\novels\\Me1.txt"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 

     String strLine; 
String s=null; 
     //Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 

      String pattern = "^[0-9]+[\\s]*$"; 
      strLine=strLine.replaceAll(pattern, " "); 
     writeResult("C:\\Users\\Vivek\\Desktop\\novels\\doci.txt",strLine); 

     } 


     //Close the input stream 
     br.close(); 

    } 

    public static void writeResult(String writeFileName, String text) 
    { 
      File log = new File(writeFileName); 
      try{ 
      if(log.exists()==false){ 
        System.out.println("We had to make a new file."); 
        log.createNewFile(); 
      } 
      PrintWriter out = new PrintWriter(new FileWriter(log, true)); 
      out.append(text); 
      out.println(); 
      out.close(); 
      }catch(IOException e){ 
       System.out.println("COULD NOT LOG!!"); 
      } 
    } 

plz幫助我。

+0

但如果文件中有1個空行也被除去,我不想 –

+0

這種情況下,如何檢查在'如果有新的空行'並增加計數。如果計數> = 1,則只寫入1個新行。 – SkrewEverything

+0

僅供參考...打開和關閉每行輸出文件效率非常低。 –

回答

1

您可以像SkrewEverything建議的那樣使用您的方法中的後續空行計數器。

或進行後處理用正則表達式是這樣的:

package testingThings; 

import java.awt.Desktop; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.io.UnsupportedEncodingException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 

public class EmptyLinesReducer { 
    public Path reduceEmptyLines(Path in) throws UnsupportedEncodingException, IOException { 
     Path path = Paths.get("text_with_reduced_empty_lines.txt"); 

     String originalContent = new String(Files.readAllBytes(in), "UTF-8"); 
     String reducedContent = originalContent.replaceAll("(\r\n){2,}", "\n\n"); 
     Files.write(path, reducedContent.getBytes()); 

     return path; 
    } 


    public Path createFileWithEmptyLines() throws IOException { 
     Path path = Paths.get("text_with_multiple_empty_lines.txt"); 
     PrintWriter out = new PrintWriter(new FileWriter(path.toFile())); 

     out.println("line1"); 

     //empty lines 
     out.println(); 
     out.println(); 
     out.println(); 
     out.println("line2"); 

     //empty lines 
     out.println(); 

     out.println("line3"); 

     //empty lines 
     out.println(); 
     out.println(); 
     out.println(); 
     out.println(); 
     out.println(); 
     out.println("line4"); 

     out.close(); 

     return path; 
    } 

    public static void main(String[] args) throws UnsupportedEncodingException, IOException { 
     EmptyLinesReducer app = new EmptyLinesReducer(); 

     Path in = app.createFileWithEmptyLines(); 
     Path out = app.reduceEmptyLines(in); 

     // open the default program for this file 
     Desktop.getDesktop().open(out.toFile()); 

    } 

}