I hate how the Java File I/O api works.....

I was developing a little noSQL database in java, just for fun.
The basic was: every entry was a json object, separated by \n.
Every entry started with the length in bytes, so i could perform a easy read of the entry with a inputstream, followed by the entry its self..

The problem?
If i had a big file with more than like 50000 entries, to alter a entry with acceptable perfomance, i had to read every entry for matching with search, than using RandomAccessFile to mark the old entry as deleted and adding the modified one at the end.
The same for delete, it was only possible to mark the entry as deleted, so the read/alter would just not read it by reading the length(which i wrote earlier) and than use inputstream.skip with the length.

To actually delete not needed entries, i created a new file and than reading the old one and writing at the same time to the new one, with skipping the not needed entries and at the end rename the new file to the old and re creating all the streams.

Why cant i just replace a specific part of the file? WTF JAVA

  • 3
    Every other language does this fyi, even software like blender or openoffice, you dont just write to a file that already exists for security reasons.
  • 0
    status update, this was the bug ive been looking for the past 2 weeks so thanks
Add Comment