Java,需要一个while循环才能达到eof。iewhile !eof, 继续解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16945335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:27:55  来源:igfitidea点击:

Java, need a while loop to reach eof. i.e.while !eof, keep parsing

javawhile-loopeofdatainputstream

提问by john stamos

I currently have a working parser. It parses a file once(not what I want it to do) and then outputs parsed data into a file. I need it to keep parsing and appending to the same output file until the end of the input file. Looks something like this.

我目前有一个工作解析器。它解析一个文件一次(不是我想要它做的),然后将解析的数据输出到一个文件中。我需要它继续解析并附加到同一个输出文件,直到输入文件结束。看起来像这样。

try {
// my code parsing the data and appending to eof of output. (works)
}
catch (EOFException eof){
}

Everything is done except the while loop. It only parses once when I need it to keep parsing. I'm looking for a while loop function to reach eof.

除了while循环之外,一切都完成了。它只在我需要它继续解析时解析一次。我正在寻找一个 while 循环函数来达到 eof。

I'm also using a DataInputStream. Is there some sort of DataInputStream.hasNext function?

我也在使用 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){
}

回答by FThompson

Instead of looping until an EOFException is thrown, you could take a much cleaner approach, and use available().

您可以采用更简洁的方法,而不是循环直到抛出 EOFException ,并使用available().

DataInputStream dis = new DataInputStream(new FileInputStream(inFile));
while (dis.available() > 0) {
    // read and use data
}

Alternatively, if you choose to take the EOF approach, you would want to set a boolean upon the exception being caught, and use that boolean in your loop, but I do not recommend it:

或者,如果您选择采用 EOF 方法,您可能希望在捕获异常时设置一个布尔值,并在循环中使用该布尔值,但我不建议这样做:

DataInputStream dis = new DataInputStream(new FileInputStream(inFile));
boolean eof = false;
while (!eof) {
    try {
        // read and use data
    } catch (EOFException e) {
        eof = true;
    }
}

回答by Ravi Thapliyal

DataInputStreamhas a lot of readXXX()methods that do throw EOFExceptionbut the method that you're using DataInputStream.read()does notthrow EOFException.

DataInputStream有很多readXXX()方法可以 throwEOFException但您使用的方法DataInputStream.read()不会throw EOFException

To correctly identify the EOF while using read()implement your whileloop as follows

要在使用时正确识别 EOF,read()while按如下方式实现您的循环

int read = 0;
byte[] b = new byte[1024];
while ((read = dis.read(b)) != -1) { // returns numOfBytesRead or -1 at EOF
  // parse, or write to output stream as
  dos.write(b, 0, read); // (byte[], offset, numOfBytesToWrite)
}

回答by BuvinJ

If you are using FileInputStream, here's an EOF method for a class that has a FileInputStream member called fis.

如果您正在使用FileInputStream,这里有一个 EOF 方法,用于具有名为 的 FileInputStream 成员的类fis

public boolean isEOF() 
{ 
    try { return fis.getChannel().position() >= fis.getChannel().size()-1; } 
    catch (IOException e) { return true; } 
}