Search code examples
javacarriage-return

How to write a text file in java in which there is no cr character at the end of each line


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?


Solution

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