gpt4 book ai didi

java - java中如何删除文件中的空行

转载 作者:行者123 更新时间:2023-12-01 09:14:11 27 4
gpt4 key购买 nike

我正在将 pdf 文件转换为文本并删除具有页码的行,但问题是它留下了 2 行的空白。所以我想删除这些连续有 2 个或更多空行的空格,但如果不是1 行是空的。我的代码是:

// 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!!");
}
}

请帮助我。

最佳答案

您可以在您的方法中使用连续的空行计数器,如 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());

}

}

关于java - java中如何删除文件中的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40693895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com