EOFException - 如何处理? [英] EOFException - how to handle?

查看:19
本文介绍了EOFException - 如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 Java 初学者,学习了 java 教程.

I'm a beginner java programmer following the java tutorials.

我正在使用 Java 教程数据流页面,在运行时,它一直显示 EOFException.我想知道这是否正常,因为读者最终必须走到文件的末尾.

I am using a simple Java Program from the Java tutorials's Data Streams Page, and at runtime, it keeps on showing EOFException. I was wondering if this was normal, as the reader has to come to the end of the file eventually.

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);
    }
}

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

It compiles fine, but the output is:

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 教程数据流页面,它说:

注意 DataStreams 通过捕获 EOFException,而不是测试无效的返回值.DataInput 方法的所有实现都使用 EOFException 而不是返回值.

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是正常的,所以只捕获它而不处理它就可以了,意味着到达文件末尾?

So, does this mean that catching EOFException is normal, so just catching it and not handling it is fine, meaning that the end of file is reached?

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

If it means I should handle it, please advise me on how to do it.

编辑

根据建议,我使用 in.available() > 修复了它.0 表示 while 循环条件.

From the suggestions, I've fixed it by using in.available() > 0 for the while loop condition.

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

Or, I could do nothing to handle the exception, because it's fine.

推荐答案

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

While reading from the file, your are not terminating your loop. So its read all the values and correctly throws EOFException on the next iteration of the read at line below:

 price = in.readDouble();

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

If you read the documentation, it says:

抛出:

EOFException - 如果此输入流在读取八个字节之前到达末尾.

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

IOException - 流已关闭且包含的输入流在关闭后不支持读取,或发生其他 I/O 错误.

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

在你的 while 循环中放置一个适当的终止条件来解决这个问题,例如下面:

Put a proper termination condition in your while loop to resolve the issue e.g. below:

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

这篇关于EOFException - 如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆