I want to check if the last line of a file is new line terminated in Java. If it is new line terminated then my program can continue but if not then I have to throw an error.
The problem is I don't know in advance what the system is. Without knowing this, the new line terminated can be '\n' or '\r\n'. I know I can use System.lineSeparator()
to get the new line termination of the system but I can't think of any way to check if the last line of the file is new line terminated.
For example, the file can contain the following input:
String input = "The quick brown " + System.lineSeparator() + "jumps over " + System.lineSeparator() + "the lazy dog." + System.lineSeparator();
How can I check if the last line(the lazy dog) is new line terminated?
I tried using BufferedReader.readLine() but it excludes any new line termination so I can't check after reading the line in. I know BufferedReader.read() will preserve the new line termination but it reads every character byte and I'm not sure how to check for new line termination without knowing the systems in advanced. Also I'm not sure how to detect if I'm reading the last line if I am using BufferedReader.read() or how to get the last 2 or 1 character?
Is there any other input stream I can use to do this?
I do not want to append a new line when reading in but I just want to check if the original file's last line is new line terminated.