0

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(packet);
  out.flush ();
}
11
  • 1
    how are you writing the data to that file? what properties does your "object" have? Commented Jun 11, 2011 at 7:29
  • @quamar, I took the liberty of editing your question so that it makes more sense to a native English speaker. I hope that's what you intended to ask, but clearer? If not then please feel free to edit your question... and even change it back. Commented Jun 11, 2011 at 7:32
  • 1
    We'll need to see samples of both the input and output files in order to make any educated guesses as to where the bloat arises. If you where, say, reading tab-delimited files, and writing XML then you'd have your explanation. Commented Jun 11, 2011 at 7:34
  • @corlettk: I prefer to try and get people to learn instead of doing the translation for them. Give that a shot next time and see if they can actually produce correct English. Still, good work, for the question does actually read better due to the edits. Commented Jun 11, 2011 at 7:38
  • Show some code please - ideally a short but complete program demonstrating the problem. There's not enough information here to answer you. Commented Jun 11, 2011 at 7:38

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.