【发布时间】:2026-01-26 06:45:02
【问题描述】:
我想知道是否有可能添加 使用文件通道的位置方法将字节缓冲区保存到文件末尾。
我读到需要打开文件输出流 带有附加标志
ByteBuffer byteBuffer = ...;
FileOutputStream fileOutputStream = new FileOutputStream("path/to/file", true);
FileChannel channel = fileOutputStream.getChannel();
channel.write(bytebuffer);
channel.force(true);
channel.close();
但是不应该可以附加缓冲区吗 通过修改通道的位置。
"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);
我将不胜感激,因为文件 被覆盖。
【问题讨论】:
-
显示证明它没有按规定工作的代码。
-
第二个代码示例中没有为 FileOutputStream 设置附加标志是错字吗?
标签: java bytebuffer filechannel