2

I'm appending data to a text file using java in unix openjdk version 1.8.0_151 using the Files.write() method.

I read this question which advised me to use System.lineSeparator() instead of "\n".

My code looks like this:

try {
    Files.write(Paths.get("spillInfo.txt"), 
    (infoString + System.lineSeparator()).getBytes(), 
     StandardOpenOption.APPEND);

} catch (IOException e) {
     System.err.println("Caught IOException: " +  e.getMessage());
}

From this question Notepad uses the ANSI charset and from this question that charset in java is "cp1252" but when I add "cp1252" or "windows-1252" as the arugment of getBytes() (or if I leave it empty like in the code above) the new lines do not show up in notepad (they do show up in notepad++ on all three counts).

CSStudent7782
  • 618
  • 1
  • 5
  • 21

1 Answers1

1

If you run your Java program on Unix, System.lineSeparator() is \n anyway.

If you need to have Windows Notepad to have windows line separator, then use \r\n.

It is old thing in Windows Notepad - it recognizes only Windows \r\n. Nobody in Microsoft cares about it (and never did) :-)

Something else you can do is to introduce your own system variable and pass it to your Java program, like

-Dtarget_system=Windows     // or Unix

then in the code you can use proper line separator based on value of

System.getProperty("target_system")
Vadim
  • 4,027
  • 2
  • 10
  • 26