我目前有一个正在工作的解析器。它解析一次文件(不是我想要的),然后将解析后的数据输出到文件中。我需要它继续解析并附加到同一输出文件,直到输入文件末尾。看起来像这样。
try {
// my code parsing the data and appending to eof of output. (works)
}
catch (EOFException eof){
}
除了 while 循环之外,一切都已完成。当我需要它继续解析时,它只解析一次。我正在寻找一个 while 循环函数来达到 eof。
我也在使用 DataInputStream。是否有某种 DataInputStream.hasNext 函数?
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){
}
Warning: 这个答案是错误的。请参阅评论以获取解释。
您可以采取更简洁的方法,而不是循环直到抛出 EOFExceptionavailable().
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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)