DataInputStream和DataOutputStream:多次读写



我在下面的例子中遇到了一个奇怪的问题。在第一次"迭代"一切工作正常,但在"--------"之后,当我试图再次写入相同的输出时,我无法再读取任何内容。我很确定我错过了什么。你能帮帮我吗?

谢谢西蒙

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

}

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

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

同样,你应该在你的代码中使用对象:而不是有3个数组5个元素(价格,单位和descs),你应该有一个5个产品的单一数组,每个产品是一个对象,有一个价格,一个描述和一个单位。Java是一种面向对象语言。使用对象。

您在这行创建您的InputStream:

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

在此之后写入outStream的内容对之后可以从in中读取的内容没有影响,因为您使用此时的字节数组的内容创建了DataInputStream。所以你可以读你在第一个for循环中写的东西,但之后你在EOF,因为第二个for循环对你的DataInputStream没有影响。

我自己也遇到过;如果我是对的,这个问题;在我的情况下,它是使用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!
    }

最新更新