What are the differences between char literals '\n' and '\r' in Java?
Solution 1:
\n
is a line feed (LF) character, character code 10. \r
is a carriage return (CR) character, character code 13. What they do differs from system to system. On Windows, for instance, lines in text files are terminated using CR followed immediately by LF (e.g., CRLF). On Unix systems and their derivatives, only LF is used. (Macs prior to Mac OS X used CR, but Mac OS X is a *nix derivative and so uses LF.)
In the old days, LF literally did just a line feed on printers (moving down one line without moving where you are horizonally on the page), and CR similarly moved back to the beginning of the line without moving the paper up, hence some systems (like Windows) sending CR (return to the left-hand side) and LF (and feed the paper up).
Because of all this confusion, some output targets will accept multiple line break sequences, so you could see the same effect from either character depending on what you're outputting to.
Solution 2:
\n
is for unix \r
is for mac (before OS X)\r\n
is for windows format
you can also take System.getProperty("line.separator")
it will give you the appropriate to your OS
Solution 3:
It depends on which Platform you work. To get the correct result use -
System.getProperty("line.separator")