Use the line.separator
system property
String separator = System.getProperty("line.separator") + "#"; // concatenate the character you want
String myPortableString = "#aaa" + separator + "ccc";
These properties are described in more detail here.
If you open the source code for PrintWriter
, you'll notice the following constructor:
public PrintWriter(Writer out,
boolean autoFlush) {
super(out);
this.out = out;
this.autoFlush = autoFlush;
lineSeparator = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
}
It's getting (and using) the system specific separator to write to an OutputStream
.
You can always set it at the property level
System.out.println("ahaha: " + System.getProperty("line.separator"));
System.setProperty("line.separator", System.getProperty("line.separator") + "#"); // change it
System.out.println("ahahahah:" + System.getProperty("line.separator"));
prints
ahaha:
ahahahah:
#
All classes that request that property will now get {line.separator}#