-1

Is is possible to remove the white spaces of a String in a text file in Java? I have tried my approach but doesn't working.

public static void main(String[] args) throws IOException {
    File f = new File("ejer2.txt");
    BufferedReader br = new BufferedReader(new FileReader(f));
    String linea = br.readLine();
    linea.replaceAll("\\s", "");
    while (linea != null) {
        System.out.println(linea);
        linea = br.readLine();
    }
    br.close();
}

The only way I can get the white spaces out of the String is when I print the line out in the While loop by using the replaceAll method in the String class, but im trying to take them out of the Stringin the File, and I'm not sure if this is possible.

Max Young
  • 1,522
  • 1
  • 16
  • 42
Ricki
  • 141
  • 3
  • 16
  • Strings are immutable. – Pshemo Jun 01 '15 at 22:26
  • @Pshemo I doubt that's the problem. I think he's trying to modify the actual text file, since it doesn't look like his code tries to do anything with the value he reads. – Aify Jun 01 '15 at 22:29
  • @Aify Well, `linea.replaceAll("\\s", "");` is not going to work because strings are immutable (`replaceAll` can't affect original string, but instead it returns changed one). Also I didn't post it as answer because like you noticed it wasn't the only problem here. – Pshemo Jun 01 '15 at 22:32
  • @Pshemo I agree with what you're saying, that the code as it was posted wouldn't work anyways, because strings are immutable. Hopefully OP will realize that he has to append the return value to a stringbuilder or otherwise assign it to a String variable. – Aify Jun 01 '15 at 22:36

2 Answers2

2

Try with this:

   linea = linea.replaceAll("\\s+","")

EDIT: It is because you didn't save the value of your new string in your variable linea. You have to asign it.

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • IM getting the correct output from the console but my original txt still has white spaces.this is my issue is it possible to modify it? From java – Ricki Jun 01 '15 at 22:27
  • Then you have to print it again in the textfile. With PrinterWriter you will make your buffer connection like this `PrintWriter out = new PrintWriter("file.txt");` and then you will have to print on the file, doing this: `out.println(linea);` Notice that I put `ln` after `print` to print in the next line each time you use it. – Francisco Romero Jun 01 '15 at 22:33
0

If you want to actually replace the spaces in the file, you need to write to the file instead of just reading from it.

You'll need to add a linea.replaceAll line inside your while loop.

You'll need to store all these lines as well - I suggest using a Stringbuilder and adding everything you read to the builder (after you run replaceAll).

You'll also need to write the final text to the text file. I suggest using a PrintStream.

eg: PrintStream out = new PrintStream(new FileOutputStream("finalFile.txt"));

then out.print(yourStringBuilder.toString())

Aify
  • 3,543
  • 3
  • 24
  • 43