gpt4 book ai didi

Java:使用 FileChannel 写入文件会使文件缩小?

转载 作者:行者123 更新时间:2023-11-30 05:53:21 25 4
gpt4 key购买 nike

我尝试使用 FileChannel 将特定字节写入文件的特定位置。但实际上文件缩小到我写更改的最后位置。我这样做:

    Path path = Paths.get("I://music - Copy.mp3");

System.out.println(Files.size(path)/1024 + "KB");

try (FileChannel chan = new FileOutputStream(path.toFile()).getChannel()) {
chan.position(1024 * 1024);

ByteBuffer b = ByteBuffer.allocate(1024);
chan.write(b);

System.out.println("Write 1KB of data");
}

System.out.println(Files.size(path)/1024 + "KB");

这是我得到的输出:

3670KB
Write 1KB of data
1025KB

谁能告诉我哪里出错了??

最佳答案

你错过了 FileOutputStream constructor允许附加到文件。如果您按上述方式创建它,则会覆盖文件的内容。

关于Java:使用 FileChannel 写入文件会使文件缩小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10633814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com