您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

:此答案不正确。请参阅注释以获取解释。

与其循环直到抛出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;
    }
}
java 2022/1/1 18:28:18 有585人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶