在java中删除数据文件的一部分 [英] Deleting parts of a data file in java

查看:259
本文介绍了在java中删除数据文件的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,我写了一些Java对象。写完之后,有时候我需要删除一些放在文件中ramndom位置的对象。有没有办法从文件中删除选定的对象,而无需更换整个文件?



谢谢。


最简单的方法删除文件的一部分就是将你想要的位拷贝到一个新文件中。如果你只是在结束时删除位,你可能会避免再次复制它的开始,但这将需要更复杂的代码。



对象流,你可能最好只从一个流中读取对象,然后把你想要的对象写入一个新对象。 (显然你可以重命名后面的输出文件。)通过自己查看序列化格式,你可以避免重复的反序列化/序列化所需的一些处理,但是我会选择简单的路径来开始。 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆