I'm given two text files, one uses \n for line breaks, the other uses \r\n. I Just want to be able to check which of these two it is. I want to use BufferedReader to read character by character and search for a \ (using \) and then check what the next character is, but when I search through the txt file for any \s, I don't get any results, the loop just ends.
For now, I'm just trying to test by printing out the \ and whatever character follows it, so I should be printing out a bunch of \n or \r when I run the file, but I just get the Done to print. Any advice?
char x;
while(reader.ready())
{
x = (char) reader.read();
if(x == '\\')
System.out.println(x + "" + (char) reader.read());
}
System.out.println("Done");
You are confusing one character \r
and a string consisting of two characters \
and r
. The code below should work for you:
char charValue;
while ((charValue = (char) reader.read()) != -1) {
if ('\n' == charValue) {
System.out.println("\\n");
}
if ('\r' == charValue) {
System.out.println("\\r");
}
}