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;
}
closeremoved) tries to keep reading forever.