简体   繁体   English

如何从java中的文本文件中删除空行

[英]How to remove blank lines from a text file in java

I am trying to duplicate the original into a new file. 我正在尝试将原始文件复制到新文件中。 In the new file I want the exact same things as the original BUT no blank lines. 在新文件中,我想要与原始文件完全相同但没有空白行。

Note: I looked at other posts and tried with no success. 注意:我查看了其他帖子,但没有成功。

Currently: 目前:

1  

2  

3  

How I want it to be: -- no blank lines 我希望如何: - 没有空行

1  
2  
3

Here is my code so far: 到目前为止,这是我的代码:

   inputFileName = "x.txt";
   outputFileName = "y.txt";

   inputFile = new BufferedReader(new FileReader(inputFileName));
   outputFile = new PrintWriter(new FileWriter(outputFileName));

   String lineOfText = inputFile.readLine();

   while(lineOfText != null)
   {
       if (lineOfText.isEmpty())
       {
        outputFile.print("null");
       }

       outputFile.println(lineOfText);
       lineOfText = inputFile.readLine();
   } 

   inputFile.close();
   outputFile.close();
}

Thank you for all who can possibly help. 感谢所有可能提供帮助的人。 I assumed that print("null") would print out 'nothing' but it indeed prints out null, I do not know how to print out 'nothing'. 我认为print("null")会打印出“nothing”,但它确实打印出null,我不知道如何打印出“nothing”。

You need to skip the println in case the line is empty: 如果该行为空,您需要跳过println

while(lineOfText != null)
{
   if (!lineOfText.isEmpty()) {
       outputFile.println(lineOfText);
   }
   lineOfText = inputFile.readLine();
 }

You're on the right track, but this 你走在正确的轨道上,但是这个

while(lineOfText != null)
{
   if (lineOfText.isEmpty())
   {
    outputFile.print("null");
   }
   outputFile.println(lineOfText);
   lineOfText = inputFile.readLine();
} 

shouldn't be writing null on empty lines. 不应该在空行上写null I think you wanted something like 我想你想要的东西

while(lineOfText != null)
{
   if (!lineOfText.isEmpty())
   {
      outputFile.println(lineOfText);
   }
   lineOfText = inputFile.readLine();
} 

Also , I suggest you use a try-with-resources Statement instead of manually managing your close (s). 此外 ,我建议您使用try-with-resources语句而不是手动管理您的close It's probably a good idea to trim (as suggested in the comments) before your test, and you can simplify your loop and you should limit variable visibility. 在测试之前trim (如评论中所示)可能是个好主意,并且您可以简化循环,并且应该限制变量可见性。 All together like, 一起像,

String inputFileName = "x.txt";
String outputFileName = "y.txt";

try (BufferedReader inputFile = new BufferedReader(new FileReader(inputFileName));
        PrintWriter outputFile = new PrintWriter(new FileWriter(outputFileName))) {
    String lineOfText;
    while ((lineOfText = inputFile.readLine()) != null) {
        lineOfText = lineOfText.trim();
        if (!lineOfText.isEmpty()) {
            outputFile.println(lineOfText);
        }
    }
}
public static void main(String[] args) {

        Scanner file;
        PrintWriter writer;

        try {

            file = new Scanner(new File("src/data1.txt"));
            writer = new PrintWriter("src/data2.txt");

            while (file.hasNext()) {
                String line = file.nextLine();
                if (!line.isEmpty()) {
                    writer.write(line);
                    writer.write("\n");
                }
            }

            file.close();
            writer.close();

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM