I want my program to do something when it finds the end of a file (EOF) at the end of the last line of text, and something else when the EOF is at the empty line AFTER that
You can use BufferedReader.read(char[] cbuf, int off, int len)
method. When end of file is reached, return value -1
, you can check if the last buffer read ended with a line separator.
Admittedly, the code would be more complicated as it will have to manage the construction of lines from the read char[]
buffers.
You'll have to use read
rather than readLine
and handle end-of-line detection yourself. readLine
considers \n
, \r
, and EOF all to be line terminators, and doesn't include the terminator in what it returns, so you can't differentiate on the basis of the returned string.
Why not use
if (line.length()==0) {
System.out.println("Found an empty line.");
}
Note: this will detect a blank line anywhere in the file, not just at EOF.
public ArrayList<String> readFile(String inputFilename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(inputFilename));
ArrayList<String> lines = new ArrayList<>();
String currentLine = "";
int currentCharacter = br.read();
int lastCharacter = -1;
// Loop through each character read.
while (currentCharacter != -1) {
// Skip carriage returns.
if (currentCharacter != '\r') {
// Add the currentLine at each line feed and then reset currentLine.
if (currentCharacter == '\n') {
lines.add(currentLine);
currentLine = "";
} else {
// Add each non-line-separating character to the currentLine.
currentLine += (char) currentCharacter;
}
}
// Keep track of the last read character and continue reading the next
// character.
lastCharacter = currentCharacter;
currentCharacter = br.read();
}
br.close();
// If the currentLine is not empty, add it to the end of the ArrayList.
if (!currentLine.isEmpty()) {
lines.add(currentLine);
}
// If the last read character was a line feed, add another String to the end
// of the ArrayList.
if (lastCharacter == '\n') {
lines.add("");
}
return lines;
}
I tried reading from a BufferedReader that received its input from a socket input stream.
Everything worked fine until the last line, where the readLine()
would just simply hang because the browser wouldn't send a newline terminator on post data.
This is my solution, to be able to read until the end of the input stream.
public String getLine(BufferedReader in)
{
StringBuilder builder = new StringBuilder();
try {
while(in.ready()) {
char input = (char)in.read();
/**
* This method only matches on " \r\n" as a new line indicator.
* change as needed for your own line terminators
*/
if(input == '\r') {
/** If we can read more, read one more character
* If that's a newline, we break and return.
* if not, we add the carriage return and let the
* normal program flow handle the read character
*/
if(in.ready()) {
input = (char)in.read();
if(input == '\n') {
break;
}
else {
builder.append('\r');
}
}
}
builder.append(input);
}
}
catch(IOException ex) {
System.out.println(ex.getMessage());
}
return builder.toString();
}