简体   繁体   中英

java binary files operations

I have a bunch of different objects(and objec types) that i want to write to a binary file. First of all i need the file to be structured like this:

`Object type1

obj1, obj2 ...

Object type2

obj1, obj2...

....

Being a binary file this doesn't help a user read it, but i want to have a structure so i can search, delete or add an object by it's type, not parsing the entire file. And this is something i don't know how to do. Is this even posible?

You will have to maintain a header at the beginning of the file (or somewhere else) to mark the position and length of each of your objects.

The kind and layout of the header depend a lot on how you plan to read and write into the file. For example if you plan to retrieve the objects by name, you could have in your file something like this

object1 500 1050
object2 1550 800
object3 2350 2000
<some padding to cover 500 bytes>
<the 1050 bytes of object1><the 800 bytes of object2><the 2000 bytes of object3> 

And know that object1 starts at the offset 400 in the file, and has a length of 1050 bytes.

Since it seems that you have different types of objects that you want to store, you will probably need to add some additional data to your header.

Take care of the following:

  • Each time you add, delete or modifiy a file, you will have to update in the header the offset for all files that follow (for example if I remove object2, then the offset for object3 is now 1550).
  • If you store the header in the same file as the data, then you must take the size of the header into account when computing offsets (this will make things much harder, I suggest you keep the header and binary data separated.
  • You will have to read and parse the header each time you want to access an object. Consider using a standardized format for your header to avoid problems (YML or XML).

I'm not aware of any library that will help you implement such a feature but I'm pretty sure there are some. Maybe someone will be able to suggest one.

--

Another solution would be to use something like a ZipFile (which is natively supported by Java) and write each of your objects as a differenz ZipEntry. This way you won't have to manage object separation yourself, and will only need to worry about knowing the exact ZipEntry you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM