Java RandomAccessFile 从头开始​​截断

发布于 2024-12-23 13:45:55 字数 267 浏览 3 评论 0原文

我知道如何截断 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2024-12-30 13:45:56

大多数文件系统不支持该操作。该模型是从光盘上的特定位置开始的字节序列。文件的长度是可变的并且可以附加,因此截断相对容易。

因此,您实际上需要复制文件中的所有字节。如果可能的话避免。管理队列文件(例如日志)的一种技术是拥有一系列文件,然后定期启动一个新文件并从末尾删除一个文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文