How to remove all white spaces from a file in Java

How to remove all white spaces from a file in Java:

In this post, we will learn how to remove all white spaces from a text file in Java. The program will take one text file as the input, read the contents of the file and modify it by removing all white spaces from the file.

We will use a regular expression to match all blank spaces. With the replaceAll method, we can remove these blank spaces.

How to match and remove all white spaces of a string in Java:

We can use the regular expression \s to match all white-space characters. It matches all white spaces including newline characters. We can use the replaceAll method of Java string to replace all blank spaces with empty strings. This method is defined as below:

public String replaceAll(String regex, String replacement)

The first parameter, regex is the regular expression to match in the string. The second parameter, replacement is the string to replace where it is matched with the regular expression.

It returns a new string by replacing all regular expressions with the replacement string.

Let’s take a look at the below example:

public class Main {

    public static void main(String[] args) {
        String s = "Hello World\t!!\n";

        System.out.print("Before: " + s);

        s = s.replaceAll("\\s", "");
        System.out.print("After: " + s);
    }
}

The regular expression we are passing in this example matches all white spaces in the string s and replaces these with an empty string, "". If you run this program, it will print:

Before: Hello World	!!
After: HelloWorld!!

As you can see in this example, all white spaces of the string s are replaced with an empty string. So, if we read all lines of the file one by one, we can use the above method to remove all white spaces from all of the strings of that file.

How to read the content of a text file line by line in Java:

The above topic explained how to match all white spaces and how to remove these from a string in Java. Let’s learn how to read and print the text file content line by line.

The easiest way to read the text file content is to use a java.io.BufferedReader object. We need to pass a FileReader object to the constructor of BufferedReader. It will buffer the file content input. Once it is done, we can use its readLine method to read a line. On each readLine method call, it will return the next line. It returns a string terminated by a newline character.

For example:

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

public class Main {

    public static void main(String[] args) {
        String file = "/Users/projects/testfile.txt";

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            for (String line; (line = br.readLine()) != null; ) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The testfile.txt contains the below content:

Hello World!!
Hello !!
Hello Again !!

If you run this program, it will print this content. Java print file content example

Removing all white spaces from a file:

We can use the above two methods to read a file and remove all white spaces from that file. We need to:

  • Read the file content line by line.
  • Open another empty file to write the content.
  • For each line,

    • Remove all white spaces.
    • Write the line to the new file.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        String input = "/Users/projects/testfile.txt";
        String output = "/Users/projects/emptyfile.txt";

        try (BufferedReader br = new BufferedReader(new FileReader(input)); FileWriter writer = new FileWriter(output)) {
            for (String line; (line = br.readLine()) != null; ) {
                writer.write(line.replaceAll("\\s", "").concat("\n"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • It creates one BufferedReader and one FileWriter to read and write the file content. It reads the contents from the testfile.txt file and writes it to the emptyfile.txt file.
  • It uses a for loop to read the file content one by one, replaces all white spaces with an empty string and appends a new line to the end of this new string.
  • The FileWriter object writes this line to the output file.

If you run this program with the below text file:

Hello World!!
Hello !!
Hello Again !!

It will write the following content to the output file:

HelloWorld!!
Hello!!
HelloAgain!!

You might also like: