我试图读取一组结构化数据温度测量。数据以5个整数(然后转换为日期)和1个双精度(温度)的集合写入二进制(dat)文件。从二进制文件读取结构化数据的循环 - Java
我能够从第一个度量中读取,但是我想通过使用循环来浏览其他度量。我知道它保存在文件中,因为我没有循环检查它。
我得到StreamClosedException
与下面的代码,这是清楚的,但JDK不允许我放置循环后关闭声明,我没有另一个想法如何做到这一点。将感谢帮助。
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;
}
我假设你想要*停止*阅读,当你到达文件的结尾,对吧?你的代码(删除close)试图永远保持阅读。 – immibis 2015-02-11 07:14:20