java 去掉空行,如何在java中删除文件中的空行

I am converting a pdf file to text and removing lines which have page number but the problem is that it leaving an empty space of 2 line.So i want to remove these spaces which have 2 or more empty line continuously but not if 1 line is empty.my code is :

// 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 help me.

解决方案

You can work with sequent empty line counter in your method like SkrewEverything suggested.

Or make a post-processing with regular expressions like this:

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());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值