简体   繁体   English

Java RandomAccessFile从开始截断

[英]Java RandomAccessFile truncate from start

I know how to truncate a RandomAccess file so that bytes at the end are removed. 我知道如何截断RandomAccess文件,以便删除末尾的字节。

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? 但是,如何截断RandomAccessFile的方式是删除开始的字节? 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. 提前致谢。

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. 一种管理队列文件(例如日志)的技术是具有一系列文件,然后定期启动一个新文件,然后将其放下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM