java - EOFException - 如何处理?

标签 java exception try-catch

我是一名 Java 初学者,关注 java tutorials .

我正在使用 Java tutorials 中的一个简单 Java 程序的Data Streams Page ,并且在运行时,它一直显示 EOFException。我想知道这是否正常,因为读者最终必须到达文件的末尾。

import java.io.*;

public class DataStreams {
    static final String dataFile = "F://Java//DataStreams//invoicedata.txt";

    static final double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
    static final int[] units = { 12, 8, 13, 29, 50 };
    static final String[] descs = {
        "Java T-shirt",
        "Java Mug",
        "Duke Juggling Dolls",
        "Java Pin",
        "Java Key Chain"
    };
    public static void main(String args[]) {
        try {
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));

            for (int i = 0; i < prices.length; i ++) {
                out.writeDouble(prices[i]);
                out.writeInt(units[i]);
                out.writeUTF(descs[i]);
            }

            out.close(); 

        } catch(IOException e){
            e.printStackTrace(); // used to be System.err.println();
        }

        double price;
        int unit;
        String desc;
        double total = 0.0;

        try {
            DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));

            while (true) {
                price = in.readDouble();
                unit = in.readInt();
                desc = in.readUTF();
                System.out.format("You ordered %d" + " units of %s at $%.2f%n",
                unit, desc, price);
                total += unit * price;
            }
        } catch(IOException e) {
            e.printStackTrace(); 
        }

        System.out.format("Your total is %f.%n" , total);
    }
}

它编译得很好,但输出是:

You ordered 12 units of Java T-shirt at $19.99
You ordered 8 units of Java Mug at $9.99
You ordered 13 units of Duke Juggling Dolls at $15.99
You ordered 29 units of Java Pin at $3.99
You ordered 50 units of Java Key Chain at $4.99
java.io.EOFException
        at java.io.DataInputStream.readFully(Unknown Source)
        at java.io.DataInputStream.readLong(Unknown Source)
        at java.io.DataInputStream.readDouble(Unknown Source)
        at DataStreams.main(DataStreams.java:39)
Your total is 892.880000.

来自 Java tutorialsData Streams Page ,它说:

Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.

那么,这是否意味着捕获EOFException是正常的,所以只捕获它而不处理它就可以了,意味着到达文件末尾?

如果这意味着我应该处理它,请告诉我如何处理。

编辑

根据建议,我已将 in.available() > 0 用于 while 循环条件。

或者,我无法处理异常,因为它很好。

最佳答案

从文件中读取时,您并没有终止循环。因此,它读取了所有值并正确在下一次读取迭代中抛出 EOFException 如下行:

 price = in.readDouble();

如果您阅读文档,它会说:

Throws:

EOFException - if this input stream reaches the end before reading eight bytes.

IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

在您的 while 循环中添加适当的终止条件以解决问题,例如下面:

     while(in.available() > 0)  <--- if there are still bytes to read

关于java - EOFException - 如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18451232/

相关文章:

java - 使用 Eventbus 重用事件对象

c++ - STDOUT 重定向后,CloseHandle 在 Studio 2010 中抛出异常

整个类的 PHP Try Catch

Java 数组列表和 Black Jack 游戏

java - 按字符串对链表进行排序

java - AES-256 可以在 API 级别 26+ 的 Android 设备上工作吗?

C++: 跨线程异常处理问题与 boost::exception

Javascript try...catch...else...终于像 Python、Java、Ruby 等

Javascript try...catch...else...finally 像 Python、Java、Ruby 等

node.js - NodeJS - 正确捕获异步函数的错误