Java缓冲输出流什么都不做!没有错误!没有消息!怎么了?

Java缓冲输出流什么都不做!没有错误!没有消息!怎么了?,java,Java,我正在从Oracle文档和课程中学习Java,这部分(文件I/O、流等)我有一些代码不起作用,我不知道为什么。我没有收到任何错误或警告,什么都没有,DataOutputStream根本不会写入文件 我尝试删除了BufferedOutputStream,它就是这样工作的,所以我猜问题出在缓冲流上,但我不知道为什么 也许有什么东西不见了。我真的卡住了 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; im

我正在从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!");
        }       
    }   
}
import java.io.BufferedInputStream;
导入java.io.BufferedOutputStream;
导入java.io.DataInputStream;
导入java.io.DataOutputStream;
导入java.io.EOFException;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
公共课校长{
静态最终字符串dataFile=“invoicedata.txt”;
静态最终双[]价格={19.99,9.99,15.99,3.99,4.99};
静态最终int[]单位={12,8,13,29,50};
静态最终字符串[]描述={
“爪哇T恤”,
“爪哇杯”,
“杜克玩杂耍玩偶”,
“Java Pin”,
“Java密钥链”
};
公共静态void main(字符串[]args)引发IOException{
//声明
DataOutputStream out=null;
DataInputStream in=null;
试一试{
out=新的DataOutputStream(新的BufferedOutputStream(新的FileOutputStream(数据文件));
in=新的DataInputStream(新的BufferedInputStream(新的FileInputStream(数据文件)));
//写作???
对于(int i=0;i
您忘记在您的
输出上调用
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();
    }
试试看{
out=新的DataOutputStream(新的FileOutputStream(dataFile));
in=新数据输入流(新文件输入流(数据文件));
//写作???
对于(int i=0;i

您还应在

中关闭您的
,并将其添加到写作部分的末尾:

    out.close();
这是阅读部分的结尾

    in.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;
    }
}

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

刷新()或关闭()


退出程序前关闭流。

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

您也可能希望写入/读取关闭的流。Java根本不会通知您,没有错误,没有消息,什么都没有。因此,请确保您对开放流进行写入/读取。

您需要使用
close()
和/或
.close()
方法关闭最后的流?噢。正确的。。完全忘记关店了。。。我现在就试试,谢谢you@GabrielMatusevich我已经添加了一个建议,建议您如何使主要方法更干净,更面向对象。太棒了!那只是一个口头的例子:3。。。但这真的很有帮助:)我有一个离题的问题!,但是你似乎真的很了解你的Java:3如果你看了我的个人资料,如果不适合这个论坛,你可以给我发邮件问我一个问题。哈哈。。。。就在那里。。。天啊。。我觉得很愚蠢:)我忘了关上。。。现在它工作了!!谢谢你,ppl。。如此快速的回复:)我如何将此帖子标记为已解决???第一次发帖?@GabrielMatusevich点击我答案旁边的绿色复选标记接受答案。
name='Java T-shirt', price=19.99, units=12
name='Java Mug', price=9.99, units=8