In Windows, the default new line character is ‘n’ . However, depending on the OS, say Unix ( also ‘n’ fortunately ), Mac, linux, the new line character might change. If you are writing an app that is supposed to work across platforms, you cannot take a chance with this. So, how do you get the OS/Platform specific new line character ?
Fortunately, Java captures this using one of its System Properties
[java title=”HelloWorld.java” gutter=”true” highlight=”1″]
Easy, right ? So, let’s say you are reading the lines in a file and want to preserve the new lines. Here is how you can use it.
[java title=”HelloWorld.java” gutter=”true” highlight=”9″]
FileReader fr = new FileReader("read_text.txt");
BufferedReader br = new BufferedReader ( fr);
//read each line of the file
while ( ( temp = br.readLine()) != null)
} catch (IOException e)
System.out.println (" Error during file read -> " + e);
This way, you can be sure that you are getting the right OS specific line delimiter. Ask your Java Training instructor to explain more about System Properties.