- Java 6 and before
1
System.getProperty("line.separator");
- [Update For] Java 7 and above
1
System.lineSeparator()
- BufferedWriter
1
2
3
4
5
6
7
8
9
StringWriter stringWriter = new StringWriter();
BufferedWriter bufferedWriter = new BufferedWriter(stringWriter);
final String data = "hello";
bufferedWriter.write(data, 0, data.length());
bufferedWriter.newLine();
bufferedWriter.newLine();
bufferedWriter.write(data, 0, data.length());
bufferedWriter.flush();
System.out.print(stringWriter.getBuffer());
- if commons-lang is already being used in the project
1
SystemUtils.LINE_SEPARATOR