Java 从文本文件中删除多余的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24256195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:06:54  来源:igfitidea点击:

removing extra white spaces from text files

javaregexnewlineremoving-whitespace

提问by user3001418

I have number of text files in the following format:

我有以下格式的文本文件数:

196903274115371008    @266093898 

Prince George takes his first public steps with his mom,                              Catherine, Duchess of    

Cambridge.

I would like to remove all extra while spaces + new line characters except the first new line characters. So I would like to above to be like this:

我想删除除第一个换行符之外的所有额外空格 + 换行符。所以我想上面是这样的:

196903274115371008@266093898 

Prince George takes his first public steps with his mom, Catherine, Duchess of Cambridge.

I wrote the following code :

我写了以下代码:

package remove_white_space222;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class Remove_white_space222 {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        FileReader fr = new FileReader("input.txt"); 
        BufferedReader br = new BufferedReader(fr); 
        FileWriter fw = new FileWriter("outfile.txt"); 
        String line;

        while((line = br.readLine()) != null)
        { 
            line = line.trim(); // remove leading and trailing whitespace
            line=line.replaceAll("\s+", " ");
            fw.write(line);


        }
        fr.close();
        fw.close();
    }

}

Thanks in advance for your help,,,,

在此先感谢您的帮助,,,,

回答by Martin Konecny

Here's one approach:

这是一种方法:

public static void main(String[] args) throws IOException {
       FileReader fr = new FileReader("input.txt"); 
        BufferedReader br = new BufferedReader(fr); 
        FileWriter fw = new FileWriter("outfile.txt"); 
        String line;

        int lineNum = 0;
        while((line = br.readLine()) != null)
        { 
            //check if we are working with the first two lines 
            //(which should remain untouched)
            if (lineNum > 1) {
                //make sure we ignore any empty lines
                if (line.trim().length() > 0) {
                    //add a space to the end of each line to make 
                    //padding before we append the next line.
                    line=line.trim().replaceAll("\s+", " ") + " ";
                }
            } else {
                //remove all whitespace.
                line = line.trim().replaceAll("\s", "");
                line = line + "\n";
            }
            fw.write(line);
            lineNum++;
        }
        fr.close();
        fw.close();
}

Output:

输出:

196903274115371008@266093898 

Prince George takes his first public steps with his mom, Catherine, Duchess of Cambridge. %  

回答by Serge Ballesta

You can use status via an enum to add newlines after first line and all empty lines following it.

您可以通过枚举使用 status 在第一行和它后面的所有空行之后添加换行符。

package remove_white_space222;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter
import java.io.IOException;


public class Remove_white_space222 {

    enum Status {

        FIRST, EMPTY, NORMAL;
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {

        FileReader fr = new FileReader("input.txt"); 
        BufferedReader br = new BufferedReader(fr); 
        FileWriter fw = new FileWriter("outfile.txt"); 
        PrintWriter pw = new PrintWriter(fw);
        String line;

        while((line = br.readLine()) != null)
        { 
            line = line.trim(); // remove leading and trailing whitespace
            line=line.replaceAll("\s+", " ");
            fw.write(line);
            if (status != Status.NORMAL) {
                if ((status == Status.FIRST) || line.isEmpty()) {
                    pw.println();
                    status = Status.EMPTY;
                } else {
                    status = Status.NORMAL;
                }
            }
        }
        fr.close();
        fw.close();
    }

}

回答by Aarati Sakhare

    File file = new File("input_file.txt");
    try(BufferedReader br = new BufferedReader(new FileReader(file)); 
            FileWriter fw = new FileWriter("empty_file.txt")) {
        String st;
        while((st = br.readLine()) != null){
            fw.write(st.replaceAll("\s+", " ").trim().concat("\n"));
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

回答by Bentaye

You can keep your logic for all the lines but line 1 (the 2nd line), just stick "\n\n"in that case, so you have an empty line.

您可以保留除第 1 行(第 2 行)之外的所有行的逻辑,"\n\n"在这种情况下坚持下去,因此您有一个空行。

Also, I'd advise to open your resources in the try this way you don't have to worry about closing them

另外,我建议以这种方式打开您的资源,您不必担心关闭它们

try(FileReader fr = new FileReader("input.txt");
    BufferedReader br = new BufferedReader(fr);
    FileWriter fw = new FileWriter("outfile.txt") ) {

    String line;
    int lineNumber = 0;
    while((line = br.readLine()) != null) {
        if(lineNumber == 1) {
            line = "\n\n";
        } else {
            line = line.trim().replaceAll("\s+", " ");
        }
        fw.write(line);
        lineNumber++;
    }
}

Outputs:

输出:

196903274115371008 @266093898

Prince George takes his first public steps with his mom, Catherine, Duchess ofCambridge.

196903274115371008 @266093898

乔治王子与他的母亲剑桥公爵夫人凯瑟琳一起迈出了他的第一个公开步骤。