简体   繁体   中英

Writing BitSet to output file without overhead?

I get a line of overhead ( "java.util.BitSet" ) when writing a BitSet to an output file using ObjectOutputStream.writeObject() .

Anyway around it?

That is not an "overhead", that't the marker that lets Java figure out what type it needs to create when deserializing the object from that file.

Since ObjectInputStream has no idea what you have serialized into a file, and has no way for you to provide a "hint", ObjectOutputStream must "embed" something for the input stream to be able to decide what class needs to be instantiated. That is why it places the "java.util.BitSet" string in front of the data of your BitSet .

You cannot get around writing this marker when you use serialization capabilities built into BitSet class. If you are serializing the object into a file by itself, with no other objects going in with it, you could write the result of toByteArray() call into a file, and call BitSet.valueOf(byteArray) after reading byteArray from the file.

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