简体   繁体   中英

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'.

You need to skip the println in case the line is empty:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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