Reputation: 19
I'm trying to read from a file which works fine. But when I reach to where in the file it skips to the next line (or makes a newline), I'm trying to find the escape character '\n'
, but it never mentions it.
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
public static void main(String args[]) {
File file = new File("Directory to file");
try {
FileInputStream fis = new FileInputStream(file);
char current_char;
while (fis.available() > 0) {
current_char = (char) fis.read();
if (current_char == '\n') {
System.out.println("We found the newline!!");
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
And the file it reads from contains this:
This is the first line!
This is the second line!
Upvotes: 1
Views: 2377
Reputation: 311163
Different platforms use different characters for new lines. You should better check the character's type instead of comparing to any literal:
if (Character.getType(current_char) == Character.LINE_SEPARATOR) {
System.out.println("We found the newline!!");
}
Upvotes: 1