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

时间:2014-06-17 05:17:02

标签: java regex newline removing-whitespace

我有多种文本文件,格式如下:

196903274115371008    @266093898 

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

Cambridge.

除了第一个新行字符外,我想删除所有空格+新行字符。所以我想在上面这样:

196903274115371008@266093898 

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

我写了以下代码:

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

}

提前感谢您的帮助,,,,

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

这是一种方法:

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

输出:

196903274115371008@266093898 

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

答案 2 :(得分:0)

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

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

}

答案 3 :(得分:0)

您可以保留除第1行(第二行)以外的所有行的逻辑,在这种情况下只需粘住"\n\n",这样您就会有一个空行。

此外,我建议您尝试打开您的资源,这样您就不必担心关闭资源

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++;
    }
}

输出:

196903274115371008 @ 266093898

乔治王子与他的妈妈凯瑟琳(剑桥公爵夫人)迈出了第一步。