I currently have a working parser. It parses a file once(not what I want it to do) and then outputs parsed data into a file. I need it to keep parsing and appending to the same output file until the end of the input file. Looks something like this.
try {
// my code parsing the data and appending to eof of output. (works)
}
catch (EOFException eof){
}
Everything is done except the while loop. It only parses once when I need it to keep parsing. I'm looking for a while loop function to reach eof.
I'm also using a DataInputStream. Is there some sort of DataInputStream.hasNext function?
DataInputStream dis = new DataInputStream(new FileInputStream(inFile));
i.e. dis.read();
.
//Need a while !eof while loop
try {
// my code parsing the data and appending to eof of output. (works)
}
catch (EOFException eof){
}
If you are using
FileInputStream
, here's an EOF method for a class that has a FileInputStream member calledfis
.Instead of looping until an EOFException is thrown, you could take a much cleaner approach, and use
available()
.Alternatively, if you choose to take the EOF approach, you would want to set a boolean upon the exception being caught, and use that boolean in your loop, but I do not recommend it:
DataInputStream
has a lot ofreadXXX()
methods that do throwEOFException
but the method that you're usingDataInputStream.read()
does not throwEOFException
.To correctly identify the EOF while using
read()
implement yourwhile
loop as follows