I have a RandomAccessFile in Java where I manage some data. Simplified: At the beginning of the file, I have an index. (An 8-byte value in the dataset that represents the offset where real data can be found).
So, if I want now, where can I find dataset data no 3, for example. I read 8 bytes at offset (2 * 8). (Indexing starts at 0).
The data set itself consists of 4 bytes, which represents the size of the data set, and then all the bytes belonging to the data set.
So this works fine if I always rewrite the whole file.
It is very important here that Dataset no 3 could be written as the first record in the file, so the index is ordered, but not by itself.
If I insert a new dataset, I always add it to the end of the file. But the number of data sets that can be in one file is limited. If I can store 100 datasets in a file, there will always be 100 records in the index. If the offset read from the dataset index is 0, the dataset is new and will be added to the file.
Boo there is one case that still does not work for me. 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 is the same length as befor, I can just overwrite the old data. But if the new dataset has more bytes than the old, I will have to move all the data in the file that is behind this dataset and update the indexes for these datasets.
Any idea how to do this? Or maybe the best way to manage the storage of these data sets in a file?
PS: Yes, of course, I was thinking about using a database, but this is not applicable for my project. I really need simple files.