DataInputStream和DataOutputStream:多次读写

时间:2014-07-07 19:47:47

标签: java

我在下面的例子中遇到了一个奇怪的问题。在第一次"迭代"一切正常,但在" --------"之后,当我再次尝试写相同的输出时,我再也看不懂了。我很确定我错过了一些东西。 你能帮我吗?

由于 西蒙娜

public class Main {

static final String dataFile = "invoicedata";
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) throws Exception {
    double price;
    int unit;
    String desc;
    double total = 0.0;

    // OUT
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(
            outStream));
    o

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

    out.flush();

    // INPUT
    DataInputStream in = new DataInputStream(new BufferedInputStream(
            new ByteArrayInputStream(outStream.toByteArray())));
    System.out.println("AVAL:" +in.available());
    try {
        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 (EOFException e) {
    }

    System.out.println("AVAL:" +in.available());

    System.out.println("-------------------------");


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

    out.flush();
    System.out.println("AVAL:" +in.available());    


    try {
        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 (EOFException e) {
    }
}

}

3 个答案:

答案 0 :(得分:2)

您正在尝试从已经达到EOF的InputStream中读取数据。

InputStream是由一个包含5个产品的字节数组构成的,然后你从这个InputStream中读取所有东西,然后你开始将产品重写到OutputStream,但是根本不修改字节数组,它仍然包含它最初创建时包含的内容,以及您在第一次迭代时完全读取的内容。

此外,您应该在代码中使用对象:您应该拥有5个产品的单个数组,而不是3个元素的数组(价格,单位和descs),每个产品都是具有价格的对象,描述和一个单位。 Java是一种OO语言。使用对象。

答案 1 :(得分:1)

您可以在此行创建InputStream:

// INPUT
DataInputStream in = new DataInputStream(new BufferedInputStream(
        new ByteArrayInputStream(outStream.toByteArray())));

在此之后写入outStream的任何内容都不会影响您之后可以从in读取的内容,因为您使用字节数组的内容创建了DataInputStream那一刻。因此,您可以在第一个for循环中阅读您所写的内容,但之后您处于EOF状态,因为第二个for循环对您的DataInputStream无效。

答案 2 :(得分:-1)

我自己也遇到过;如果我是对的,这个问题;在我的情况下,当使用DataOutputStream与Java网络时。
您的问题可能出在DataOutputStream上,当它没有重置时会存储它首先发送的信息,并且总是发送相同的数据,所以我要添加:out.reset();

其次,你不是多次发送数据而你的输出不是无限循环,所以;反过来,它到达问题的最后(EOF)并停止通过OutputStream发送数据。

尝试添加:

 //OUTPUT
    while(true) {
       //For Loop Which Sends Data
       try {
       Thread.sleep(10);  //The Sleep Is Not Necessary, However It's Recommendable Because 
       }catch(InterruptedException e) {} // of the Lessening of CPU Usage!
    }
相关问题