首页 文章

FileChannel - 在eof附加字节缓冲区?

提问于
浏览
0

我想知道是否有可能通过使用文件通道的位置方法将字节缓冲区添加到文件的末尾 .

我已经读过使用append标志打开文件输出流是必要的

ByteBuffer byteBuffer = ...;
FileOutputStream fileOutputStream = new FileOutputStream("path/to/file", true);
FileChannel channel = fileOutputStream.getChannel();

channel.write(bytebuffer);
channel.force(true);
channel.close();

但是不应该通过修改 Channels 的位置来附加缓冲区 .

"The size of the file increases when bytes are written beyond its current size"

ByteBuffer byteBuffer = ...;
FileOutputStream fileOutputStream = new FileOutputStream("path/to/file");
FileChannel channel = fileOutputStream.getChannel();

channel.position(channel.size()).write(bytebuffer);
channel.force(true);

我会感激一些解释,因为文件被覆盖了 .

1 回答

  • 2

    在第二个示例中,文件被覆盖,因为您没有使用值 true 指定 append 参数 . 之后,将其定位在EOF只是将其定位为零 .

相关问题