3

I want to create a text file, than load it up without any newlines or spaces (This is for a simple RPG). So I want to test for all 3 major OS line separators, and than the current OS'(s?) one. I know I can get the current one using System.getProperty("line.separator"), but how can I get Linux, Mac, and Windows line separators and turn them into a string for Java?

Edit: I need a single character representation of these, because for some reason "\n" doesn't work (Yes I'm on windows).

William
  • 8,630
  • 23
  • 77
  • 110

3 Answers3

18

Java 7 now has a System.lineSeparator() method.

See Java: How do I get a platform independent new line character?

Community
  • 1
  • 1
czerny
  • 15,090
  • 14
  • 68
  • 96
8

They are "\n" (Linux and MacOS X), "\r" (MacOS 9 and older) and "\r\n" (Windows).

Just hard-code them - they're not going to change! :-)

Edit: There's no "single character representation" of "\r\n" - it's two characters.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
-2

there's no "getter" for these, you just need to copy the strings off the internet into your own constants.

Rob Fonseca-Ensor
  • 15,510
  • 44
  • 57