I am writing an object to a file. The object is basically a data chunk from a file, with some other information added. The thing I don't understand is that the input file is around 10 KB, but the output file is about 20 KB, but data I add one only a few bytes.
What could be the reason? Are there some object identifiers, attributes also logged and occupy some space apart from the data chunk.
Code Snippet:
public void writePacket(Packet packet) {
String fileName = packet.getName();
String filePath = dtnStore+fileName ;
ObjectOutputStream out = null;
File file = new File(filePath);
if(!file.exists())
out = new ObjectOutputStream(new FileOutputStream (filePath));
else
out = new AppendableObjectOutputStream (new FileOutputStream (filePath, true));
out.writeObject(packe开发者_如何学运维t);
out.flush ();
}
ObjectOutputStream is a reasonable verbose format. It includes a lot of information about how to reconstruct an object including its types. Most file formats don't contain this information which makes them smaller but not as convenient for a Java developer to use.
Unless your input is from an ObjectInputStream the file size can take dramatically.
e.g. say you read an Integer from a text file say 100\n
is 4 bytes, but when you writ this to a ObjectOutputSTream it will be 81 bytes long as it include a header, the type of the object, Number the field names etc.
精彩评论