【问题标题】:Java Buffered Output Stream does Nothing! No Error! No Message! What is Wrong? [duplicate]Java 缓冲输出流什么都不做!没有错误!没有消息!怎么了? [复制]
【发布时间】:2013-01-09 13:46:57
【问题描述】:

我正在从 Oracle 文档和课程中学习 Java,我学习了这一部分(文件 I/O、流等),我这里有一些代码不起作用,我不知道为什么.我没有收到任何错误或警告,什么都没有,DataOutputStream 根本不会写入文件。

我尝试删除 BufferedOutputStream 并以这种方式工作,所以我猜测问题出在缓冲流上,但我不知道为什么。

也许缺少了什么。我真的被困住了。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Principal {

    static final String dataFile = "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) throws IOException {     
        //DECLARATION
        DataOutputStream out = null;
        DataInputStream in = null;

        try {
            out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
            in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));

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

        } catch (Exception e) {
            System.err.println("ERROR!");
            e.printStackTrace();
        }

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

        //READING
        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.err.println("END OF FILE!");
        }       
    }   
}

【问题讨论】:

标签: java


【解决方案1】:

您需要在代码中添加 out.close() 以在所有内容都写入文件后关闭 DataOutputStream。

【讨论】:

    【解决方案2】:

    如果你不 close() 文件,文件的结尾可能会被截断。如果文件足够小,这可能意味着它将是空的。

    如果您使用自定义对象而不是数组,代码可能如下所示

    public static void main(String... ignored) throws IOException {
        List<Inventory> inventories = new ArrayList<>();
        inventories.add(new Inventory("Java T-shirt", 19.99, 12));
        inventories.add(new Inventory("Java Mug", 9.99, 8));
    
        String dataFile = "invoice-data.dat";
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
        out.writeInt(inventories.size());
        for (Inventory inventory : inventories)
            inventory.write(out);
        out.close();
    
        DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
        int count = in.readInt();
        for (int i = 0; i < count; i++) {
            System.out.println(new Inventory(in));
        }
        in.close();
    }
    
    static class Inventory {
        final String name;
        final double price;
        int units;
    
        Inventory(String name, double price, int units) {
            this.name = name;
            this.price = price;
            this.units = units;
        }
    
        Inventory(DataInput in) throws IOException {
            this.name = in.readUTF();
            this.price = in.readDouble();
            this.units = in.readInt();
        }
    
        public void write(DataOutput out) throws IOException {
            out.writeUTF(name);
            out.writeDouble(price);
            out.writeInt(units);
        }
    
        @Override
        public String toString() {
            return "name='" + name + '\'' +
                    ", price=" + price +
                    ", units=" + units;
        }
    }
    

    打印

    name='Java T-shirt', price=19.99, units=12
    name='Java Mug', price=9.99, units=8
    

    【讨论】:

    • 哦哦。对..完全忘记关闭...我现在就试试谢谢
    • 太棒了!那只是一个 Oracae 示例:3 ...但这真的很有帮助 :)
    • 我有一个离题的问题!,但你似乎真的了解你的 Java :3
    • 如果你查看我的个人资料,如果它不适合这个论坛,你可以给我发电子邮件。
    【解决方案3】:

    您忘记在您的out 上致电close()

        try {
            out = new DataOutputStream(new FileOutputStream(dataFile));
            in = new DataInputStream(new FileInputStream(dataFile));
    
            //WRITING???
            for (int i = 0; i < prices.length; i ++) {
                out.writeDouble(prices[i]);
                out.writeInt(units[i]);
                out.writeUTF(descs[i]);
            }
            out.close();
        } catch (Exception e) {
            System.err.println("ERROR!");
            e.printStackTrace();
        }
    

    您还应该关闭您的in

    【讨论】:

    • LoL.... 就是这样... OMG.. 我觉得自己很愚蠢 :) 我忘了关闭... 现在它可以工作了!谢谢大家..这么快回复:)
    【解决方案4】:

    将此添加到写作部分的末尾:

        out.close();
    

    至此阅读部分结束;

        in.close();
    

    【讨论】:

    • 我怎样才能将此帖子标记为已解决???第一次发帖?
    • @GabrielMatusevich 点击我的答案旁边的绿色复选标记接受答案。
    【解决方案5】:

    当进程终止时,非托管资源将被释放。对于 InputStreams 这很好。对于 OutputStreams,您可能会丢失缓冲数据,因此您应该

    flush() 或 close()

    退出程序之前的流。

    【讨论】:

      【解决方案6】:

      也可能发生您想要写入/读取已关闭流的情况。 Java 根本不会通知你,没有错误,没有消息,什么都没有。因此,请确保您对打开的流进行写入/读取。

      【讨论】:

        猜你喜欢
        • 2011-11-19
        • 2013-02-07
        • 2022-07-27
        • 1970-01-01
        • 1970-01-01
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多