why the snippet is unable to detect the end of line
FileReader reader = new FileReader("d:\\UnderTest\\AVS\\tester.txt");
char ch;
int x;
while开发者_StackOverflow( ( 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.
- What string is returned by
System.getProperty("line.separator")
? Is it multiple characters, e.g."\r\n"
? No single character will ever be equal to a string that contains more than one character. Even more fundamentally, though, the code uses
==
rather thanString.equals()
. When checking string equality, never use==
. Always useString.equals()
:FileReader reader = new FileReader("d:\\UnderTest\\AVS\\tester.txt"); char ch; int x; final String linesep = System.getProperty("line.separator"); while( (x = reader.read()) != -1 ) { if( linesep.equals(Character.toString((char)x)) ) { System.out.println("new line encountered !"); } System.out.print( (char)x ); }
I don't know from your question if cross-platform issues could be involved, but there are some differences in recognized newline characters between platforms (such as Unix and DOS) that could possibly explain this problem. I'm not sure, but I think Notepad uses "/r/n" and that may not be recognized by your code as a line separator.
Take a look at Wikipedia - newline
and specifically at this section: "The different newline conventions often cause text files that have been transferred between systems of different types to be displayed incorrectly. For example, files originating on Unix or Apple Macintosh systems may appear as a single long line on some Windows programs. Conversely, when viewing a file originating from a Windows computer on a Unix system, the extra CR may be displayed as ^M at the end of each line or as a second line break."
I hope that helps.
精彩评论