在RandomAccessFile中插入数据并更新索引 [英] Inserting data in RandomAccessFile and updating index

查看:99
本文介绍了在RandomAccessFile中插入数据并更新索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中有一个RandomAccessFile,我可以在其中管理一些数据.简化: 在文件的开头,我有一个索引. (每个数据集的8字节长值代表可以在其中找到实际数据的偏移量.)

I've got a RandomAccessFile in Java where i manage some data. Simplified: At the start of the file i have an index. (8 byte long value per dataset which represents the offset where the real data can be found).

因此,如果我现在想在哪里可以找到3号数据集的数据.我读取了8个字节的偏移量(2 * 8). (索引从0开始).

So if i want to now where i can find the data of dataset no 3 for example. I read 8 Bytes at offset (2*8). (Indexing starts with 0).

数据集本身由4个字节组成,代表数据集的大小,然后是属于该数据集的所有字节.

A dataset itsself consists of 4 Bytes which represents the size of the dataset and then all the bytes belonging to the dataset.

所以在我总是重写整个文件的情况下,效果很好.

So that works fine in case i always rewrite the whole file.

在这里非常重要,数据集3可以作为文件的第一个条目写入,因此索引是有序的,而不是数据本身.

It's pretty important here, that Dataset no 3 could have been written as the first entry in the file so the index is ordered but not the data itsself.

如果我插入一个新的数据集,我总是将其附加到文件末尾.但是可能是一个文件的数据集的数量是有限的.如果我可以在文件中存储100个数据集,则索引中将始终有100个条目.如果从数据集索引中读取的偏移量为0,则该数据集是新数据集,并将附加到文件中.

If i insert a new dataset, i always append it to the end of the file. But the number of datasets that could be i n one file is limited. If i can store 100 datasets in the file there will be always 100 entries in the index. If the offset read from the index of a dataset is 0 the dataset is new and will be appended to the file.

Bu,有一种情况对我来说还行不通.如果我阅读数据集号. 3从文件中,我在应用程序中向其中添加一些数据,我想在文件中更新它,但我不知道该怎么做.

Bu there's one case which is not working for me yet. If i read dataset no. 3 from the file and i add some data to it in my application and i want to update it in the file i have no idea how to do this.

如果它的长度与之前一样长,我可以简单地覆盖旧数据.但是,如果新数据集的字节数大于旧数据集的字节数,那么我将不得不移动该数据集后面文件中的所有数据,并更新这些数据集的索引.

If it has the same length as befor i can simply overwrite the old data. But if the new dataset has more bytes than the old one i'll have to move all the data in the file which is behind this dataset and update the indexes for these datasets.

任何想法该怎么做? 还是有更好的方法来管理将这些数据集存储在文件中?

Any idea how to do that? Or is there maybe a better way to manage storing these datasets in a file?

PS:是的,我当然想过要使用数据库,但这不适用于我的项目.我确实确实需要简单的文件.

PS: Yes of course i thought of using a database but this is not applicable for my project. I really do need simple files.

推荐答案

您无法轻松地将数据插入文件中间.您基本上必须读取所有剩余的数据,写入新"数据,然后重写旧"数据.或者,您可以使旧的慢速"无效(可能允许其在以后重用),然后将整个新记录写入文件末尾.老实说,我对您的文件格式不是很清楚,但是从根本上讲,您需要意识到您不能在文件中间插入(或删除).

You can't easily insert data into the middle of a file. You'd basically have to read all the remaining data, write the "new" data and then rewrite the "old" data. Alternatively, you could potentially invalidate the old "slow" (potentially allowing it to be reused later) and then just write the whole new record to the end of the file. Your file format isn't really clear to me to be honest, but fundamentally you need to be aware that you can't insert (or delete) in the middle of a file.

这篇关于在RandomAccessFile中插入数据并更新索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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