8

如何使用 Java SE 从 txt 文件中删除所有空格和空行?

输入:

qwe
    qweqwe
  qwe



qwe

输出:

qwe
qweqwe
qwe
qwe

谢谢!

4

8 回答 8

14

像这样的东西怎么样:

FileReader fr = new FileReader("infile.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
    if (!line.equals("")) // don't write out blank lines
    {
        fw.write(line, 0, line.length());
    }
} 
fr.close();
fw.close();

注意 - 未经测试,可能不是完美的语法,但为您提供了一个可以遵循的想法/方法。

请参阅以下 JavaDocs 以供参考:http: //download.oracle.com/javase/7/docs/api/java/io/FileReader.html http://download.oracle.com/javase/7/docs/api /java/io/FileWriter.html

于 2011-08-25T12:56:02.813 回答
4

看看 trim() 函数

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#trim()

此外,一些代码会有所帮助......

于 2011-08-25T12:52:15.217 回答
3
...
Scanner scanner = new Scanner(new File("infile.txt"));
PrintStream out = new PrintStream(new File("outfile.txt"));
while(scanner.hasNextLine()){
    String line = scanner.nextLine();
    line = line.trim();
    if(line.length() > 0)
        out.println(line);
}
...
于 2011-08-25T13:28:26.620 回答
1

这是我第一次在这个网站上回答问题,所以请理解,经过大量搜索后,我发现这对我有用。

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

while((line = br.readLine()) != null)
{ 
     line = line.trim(); // remove leading and trailing whitespace
            line=line.replaceAll("\\s+", " ").trim().concat("\n");
            if (!line.equals("")) // don't write out blank lines
    {
        fw.write(line, 0, line.length());
    }
            
         
} 
fr.close();
fw.close();
于 2020-11-08T13:06:07.287 回答
0

删除每一行的空格,不要考虑空行和空行:

String line =  buffer.readLine();

while (line != null) {
    line = removeSpaces(line);        
    //ignore empty lines
    if (line.isEmpty()) return;

      ....code....


    line =  buffer.readLine();
} 




public String removeSpaces (String arg)
{
    Pattern whitespace = Pattern.compile("\\s");
    Matcher matcher = whitespace.matcher(arg);
    String result = "";
    if (matcher.find()) {
        result = matcher.replaceAll("");
    }
    return result;
}
于 2013-11-22T10:53:53.150 回答
0

用于删除同一文件中的空行。

public static void RemoveEmptyLines(String FilePath) throws IOException
{
    File inputFile = new File(FilePath);
    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
    String inputFileReader;
    ArrayList <String> DataArray = new ArrayList<String>();
    while((inputFileReader=reader.readLine())!=null)
    {
        if(inputFileReader.length()==0)
        {
            continue;
        }
        DataArray.add(inputFileReader);
    }
    reader.close();

    BufferedWriter bw = new BufferedWriter(new FileWriter(FilePath));
    for(int i=0;i<DataArray.size();i++)
    {
        bw.write(DataArray.get(i));
        bw.newLine();
        bw.flush();
    }
    bw.close();
}
于 2017-03-09T11:21:13.027 回答
0
package com.home.interview;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class RemoveInReadFile {

    public static void main(String[] args) {

        try {
            Scanner scanner = new Scanner(new File("Readme.txt"));


            while(scanner.hasNext())
            {
                String line = scanner.next();

                String lineAfterTrim = line.trim();
                System.out.print(lineAfterTrim);
            }


        } 

        catch (FileNotFoundException e) {

            e.printStackTrace();
        }

    }

}
于 2017-10-14T11:55:02.460 回答
0

我想你只想要一个正则表达式:

        txt= txt.replaceAll("\\n\\s*\\n", "\n");  //remove empty lines
        txt= txt.replaceAll("\\s*", "");  //remove whitespaces

至于读/写文件,还有很多其他资源可以了解如何做到这一点。

于 2021-05-06T14:11:09.987 回答