Java RandomAccessFile 从头开始截断
我知道如何截断 RandomAccess 文件,以便删除末尾的字节。
raf.getChannel().truncate(file.length() - 4);
或者
raf.setLength(file.length() - 4);
但是如何以删除开头的字节的方式截断 RandomAccessFile?我不需要将此文件的内容写入新文件。我用谷歌搜索,找不到答案。请帮忙。提前致谢。
I know how to truncate a RandomAccess file so that bytes at the end are removed.
raf.getChannel().truncate(file.length() - 4);
or
raf.setLength(file.length() - 4);
But how to truncate a RandomAccessFile in such a way that bytes at the start is removed? I don't need to write contents of this file to a new file. I googled and could not find an answer. Please help. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数文件系统不支持该操作。该模型是从光盘上的特定位置开始的字节序列。文件的长度是可变的并且可以附加,因此截断相对容易。
因此,您实际上需要复制文件中的所有字节。如果可能的话避免。管理队列文件(例如日志)的一种技术是拥有一系列文件,然后定期启动一个新文件并从末尾删除一个文件。
It's not an operation most file systems support. The model is a sequence of bytes starting at a particular place on the disc. Files are variable length and can be appended, and so truncation is relatively easy from there.
So you will actually need to copy all the bytes in the file. If at all possible avoid. One technique to manage queue files (such as logs), is to have a sequence of files then start a new file periodically and drop one off the end.