FileReader reader = new FileReader("d:\\UnderTest\\AVS\\tester.txt");
char ch;
int x;
while( ( x = reader.read() ) != -1 ) {
// I use the following statement to detect EOL
if( Character.toString((char)x) == System.getProperty("line.separator") ) {
System.out.println("new line encountered !");
} System.out.print( (char)x );
}
In this code the if statement never works though in tester.txt
there are 2 sentences written on new lines.
Why is that so ?
As some have mentioned, the system property line.separator
may return more than one character, e.g. on Windows, where it's \r\n
.
Depending on your use case, you might be better off using BufferedReader::readLine()
to directly read an entire line and avoid having to perform a manual comparison.