Java, need a while loop to reach eof. i.e.while !eof, keep parsing

19,792

Solution 1

Warning: This answer is incorrect. See the comments for explanation.


Instead of looping until an EOFException is thrown, you could take a much cleaner approach, and use available().

DataInputStream dis = new DataInputStream(new FileInputStream(inFile));
while (dis.available() > 0) {
    // read and use data
}

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 dis = new DataInputStream(new FileInputStream(inFile));
boolean eof = false;
while (!eof) {
    try {
        // read and use data
    } catch (EOFException e) {
        eof = true;
    }
}

Solution 2

DataInputStream has a lot of readXXX() methods that do throw EOFException but the method that you're using DataInputStream.read() does not throw EOFException.

To correctly identify the EOF while using read() implement your while loop as follows

int read = 0;
byte[] b = new byte[1024];
while ((read = dis.read(b)) != -1) { // returns numOfBytesRead or -1 at EOF
  // parse, or write to output stream as
  dos.write(b, 0, read); // (byte[], offset, numOfBytesToWrite)
}
Share:
19,792
john stamos
Author by

john stamos

My original family name is Stamotopoulos, which was shortened by my grandparents when they came to America from Greece. My parents are Bill and Loretta (Phillips), and I have two younger sisters, Janeen & Alaina, both school teachers. My first professional role was as Blackie Parrish on the daytime soap "General Hospital" (1963) from 1982-1984, for which I won two Soap Opera Digest Awards (1982 and 1983). In 1987 I began the role of Jesse on "Full House" (1987), which is my best-known role to date, spanning eight years. At my request, the writers of "Full House" (1987) gave my character the last name of Katsopolis, instead of the original character's name, Cochran, in order to highlight my Greek heritage. I have played drums occasionally with The Beach Boys since 1985, and directed their "Hot Fun in the Summertime" video. I expanded my career to include the role of J. Pierrepont Finch in Broadway's production of "How to Succeed in Business Without Really Trying!" in 1995, showcasing not only my musical talents, but my dancing ability, as well. On September 19, 1997, I married model Rebecca Romijn of MTV's "House of Style" (1989), which was the first marriage for both. We were officially divorced on March 1, 2005.

Updated on June 08, 2022

Comments

  • john stamos
    john stamos almost 2 years

    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){
    }