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幫助我。
但如果文件中有1個空行也被除去,我不想 –
這種情況下,如何檢查在'如果有新的空行'並增加計數。如果計數> = 1,則只寫入1個新行。 – SkrewEverything
僅供參考...打開和關閉每行輸出文件效率非常低。 –