範例

public static void main( String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter( "LineSeparators.txt" ); //$NON-NLS-1$
fw.write( "Hello World!" ); //$NON-NLS-1$
fw.write("\n");//$NON-NLS-1$
}catch (Exception e){
e.printStackTrace();
} finally {
if ( fw != null ) {
try { fw.close(); } catch ( Exception e ) {}
}
}
}

解決方案
改用 System.getProperty( "line.separator" )。

public static void main( String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter( "LineSeparators.txt" ); //$NON-NLS-1$
fw.write( "Hello World!" ); //$NON-NLS-1$
fw.write( System.getProperty( "line.separator" ) ); //$NON-NLS-1$
}catch (Exception e){
e.printStackTrace();
} finally {
if ( fw != null ) {
try { fw.close(); } catch ( Exception e ) {}
}
}
}