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

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

问题描述

我是初学者java程序员,遵循 java教程



我使用的是一个简单的Java程序,从 Java教程数据流页,在运行时它继续显示 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恤,
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))); (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(); //曾经是System.err.println();
}

双价格;
int单位;
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(你订购%d+单位%s at $%2f%n,
unit,desc,price);
总+ =单位*价格;
}
} catch(IOException e){
e.printStackTrace();
}

System.out.format(你的总数是%f。%n,总计);
}
}

它编译得很好,但输出是: p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $您订购了13套Duke玩具娃娃$ 15.99
您以$ 3.99
订购了29个单位的Java Pin $ 4.99
订购了50套Java Key Chain $ java.io.EOFException
(java.io.DataInputStream.readFully .main(DataStreams.java:39)
您的总数为892.880000。

Java教程数据Streams Page ,它说:


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


那么这是否意味着捕获 EOFException 是正常的,所以只是捕捉它,而不是处理它是罚款,意味着文件的结尾达到?



如果这意味着我应该处理请告知我如何做。



编辑



从建议,我已经通过使用 in.available()> 0 循环条件。



或者,我无法处理例外,因为没关系。

解决方案

在从文件读取的时候,你没有终止你的循环。所以它读取所有的值,并且正确地在下一行的下一次迭代中抛出EOFException:

  price = in.readDouble(); 

如果您阅读文档,它说:




抛出:



EOFException - 如果此输入流在读取八个字节之前到达结束。



IOException - 流已关闭,包含的输入流在关闭后不支持读取,或发生另一个I / O错误。


在while循环中放置适当的终止条件以解决问题,例如以下:

  while(in.available()> 0)< ---如果仍然有字节读取


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

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.

From the Java tutorials's Data Streams Page, it says:

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.

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.

EDIT

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.

解决方案

While reading from the file, your are not terminating your loop. So its reads 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:

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.

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天全站免登陆