0

I'm trying to read a set of structured data temperature measures. The data is being written to binary (dat) file as set of 5 integers (then translated to Date) and 1 double (temperature).

I'm able to read from first measure, however I would like to browse through additional measures just using a loop. I know it's saved in the file, as I checked it without loop.

I get StreamClosedException with the below code, which is clear, but JDK does not allow me to place close statement after the loop, and I do not have another idea how to do this. Will appreciate help.

public String readFile() {

    String s = "";
    int y, m, d, h, mm = 0;
    double p = 0;

    try {
        FileInputStream fis = new FileInputStream(fileLocation);
        BufferedInputStream bis = new BufferedInputStream(fis);
        DataInputStream dis = new DataInputStream(bis);

        while (true) {
            y = dis.readInt();
            m = dis.readInt();
            d = dis.readInt();
            h = dis.readInt();
            mm = dis.readInt();
            temp = dis.readDouble();

            System.out.println("Measure from : " + y + "-" + m + "-" + d
                    + " " + h + ":" + mm + " ,temp: " + temp);
            dis.close();
        }

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return s;
}
1
  • I assume you want to stop reading when you get to the end of the file, right? Your code (with the close removed) tries to keep reading forever. Commented Feb 11, 2015 at 7:14

1 Answer 1

1

When using a try, catch block, there is also a finally which you can use.

This block will be called, whether the try ends successfully, or results in a caught exception. In this finally block, is where you should be closing all streams.

Using this method, you should also be declaring your needed objects outside of the block, and then doing the assignments from within. Here is a sample for using dis, you will need to add in you other objects.

DataInputStream dis;

try{

    // other stream assignments
    dis = new DataInputStream(bis);
    // write data

...
} catch (IOException ex) {

    ex.printStackTrace();
} finally {

    // close other streams
    dis.close();
}

Back to your loop, is is not good practice to have a while(true) loop, ever, instead, use a conditional boolean that has the ability to be changed from within the loop

boolean run = true;

while(run){

    // do stuff
    if ( condition ) {
        run = false;
    }
}

If you try to add the dis.close() after the while loop, with a (true) hardcoded, your compiler will throw a warning, because it knows that it should never reach that point in code. If you add a conditional while loop, that warning will go away.

Sign up to request clarification or add additional context in comments.

1 Comment

many thanks. It's useful, but still not working for me. I think I'm missing the type of condition I could use eg. dis ".hasNext byte or sth" but I cannot find this type of method in dis Object...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.