在java中删除数据文件的一部分 [英] Deleting parts of a data file in java
问题描述
谢谢。
最简单的方法删除文件的一部分就是将你想要的位拷贝到一个新文件中。如果你只是在结束时删除位,你可能会避免再次复制它的开始,但这将需要更复杂的代码。
对象流,你可能最好只从一个流中读取对象,然后把你想要的对象写入一个新对象。 (显然你可以重命名后面的输出文件。)通过自己查看序列化格式,你可以避免重复的反序列化/序列化所需的一些处理,但是我会选择简单的路径来开始。 p>
I have a file which I write some java objects. After writing, sometimes I need to delete some of these objects placed at ramndom positions in the file. Is there a way to delete a selected object from the file without replacing the whole file?
Thank you.
No, this is fundamentally something that most file systems don't support. (There may be some with special data structures to make it cheap, but most don't - and the standard APIs don't support the idea.)
The simplest way of "deleting" part of a file is to copy just the bits you want to a new file. If you're only removing bits near the end, you could potentially avoid copying the start of it again, but that would require more complicated code.
In the case of a stream of objects, you'd probably be best to just read objects from one stream, and write the ones you still want into a new one. (Obviously you can rename the output file afterwards.) It's possible that by looking at the serialization format yourself you could avoid some of the processing required by repeated deserialization/serialization, but I'd go for the simple route to start with.
这篇关于在java中删除数据文件的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!