本文介绍了如何在java中删除文件中的空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

请帮帮我.

推荐答案

您可以在建议的SkrewEverything之类的方法中使用顺序的空行计数器.

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

    }

}

这篇关于如何在java中删除文件中的空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:44