-1

I need to write a txt file in Java with no carriage return charcter at the end of each line. Already I am using PrintWriter. But the output is as follow: enter image description here

I need it to be like the following picture. enter image description here

How can I do that?

Community
  • 1
  • 1
Mahnaz
  • 3
  • 2

1 Answers1

0

One way to do this is to extend the PrintWriter class and override the println() method.

public class UnixStylePrintWriter extends PrintWriter {
    @Override
    public void println() {
        write('\n');
    }
}

Then use this instead of the normal PrintWriter.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
  • Thank you. It worked perfectly. I just added it to my code in this way: writer = new PrintWriter("file.txt", "ASCII") { @Override public void println() { write('\n'); } }; – Mahnaz Oct 16 '19 at 15:57