:此答案不正确。请参阅注释以获取解释。
与其循环直到抛出EOFException,不如使用更干净的方法并使用available()
。
DataInputStream dis = new DataInputStream(new FileInputStream(inFile));
while (dis.available() > 0) {
// read and use data
}
另外,如果您选择采用EOF方法,则希望在捕获到异常时设置一个布尔值,并在循环中使用该布尔值,但是我不建议这样做:
DataInputStream dis = new DataInputStream(new FileInputStream(inFile));
boolean eof = false;
while (!eof) {
try {
// read and use data
} catch (EOFException e) {
eof = true;
}
}