3

I am doing in Java a File Comprimir to a Binary File. The problem is the next, how can I write a Byte in a new File that the total size only occups 1 byte? I am doing the next:

FileOutputStream saveFile=new FileOutputStream("SaveObj3.sav");
        // Create an ObjectOutputStream to put objects into save file.
        ObjectOutputStream save = new ObjectOutputStream(saveFile);

        save.writeByte(0);


        save.close();
        saveFile.close();

That, only must to write a only byte in the file, but when I look the size,it occups 7 bytes. Anyone knows how can I write a only byte? Is there another way better?

3
  • 1
    What platform? How do you know it occupies 7 bytes? Commented Jun 1, 2013 at 14:29
  • 3
    I would say that probably ObjectOutputStream inserts additional informations regarding type of data (which is useful in serialization and deserialization). How does your program acts when you use saveFile.write(0); instead of save.writeByte(0);? Commented Jun 1, 2013 at 14:32
  • 2
    +1 to @Pshemo (who should make that an answer). The javadoc of the ObjectOutputStream constructor says: This constructor writes the serialization stream header to the underlying stream. Commented Jun 1, 2013 at 14:34

3 Answers 3

3

Don't use ObjectOutputStream. Use the FileOutputStream directly:

FileOutputStream out=new FileOutputStream("SaveObj3.sav");
out.write(0);
out.close();
Sign up to request clarification or add additional context in comments.

9 Comments

That will actually write an int, i.e. four bytes, because the FileOutputStream.write(int b) method will be invoked. To write a single byte, one can use the [FileOutputStream.write(byte[] b)](docs.oracle.com/javase/7/docs/api/java/io/…) by wrapping the 0 in a byte[], e.g. out.write(new byte[]{0});
@matsev: Funny: Then why tell the doc you linked: "Writes the specified byte to this file..."? And why does running the code only writes one bye? Oh, and the doc for OutputStream#write(int) tells you: "The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored."
@Runemoro: Your first version was correct also and more mininal.
@A.H. Ok... I made it write(0) again.
@A.H. Interesting, I guess I did not read the JavaDocs thoroughly. It makes you wonder why they did not make the signature FileOutputStream.write(byte b) in the first place...
|
3

As JB Nizet noticed documentation of ObjectOutputStream constructor states that this object also

writes the serialization stream header to the underlying stream

which explains additional bytes.

To prevent this behaviour you can just use other streams like FileOutputStream or maybe DataOutputStream

FileOutputStream saveFile = new FileOutputStream("c:/SaveObj3.sav");
DataOutputStream save = new DataOutputStream(saveFile);

save.writeByte(0);

save.close();

Comments

1

You can use Files class provided by Java 7. It's more easy than expected.

It can be performed in one line:

byte[] bytes = new String("message output to be written in file").getBytes();
Files.write(Paths.get("outputpath.txt"), bytes);

If you have a File class, you can just replace:

Paths.get("outputpath.txt")

to:

yourOutputFile.toPath()

To write only one byte, as you want, you can do the following:

Files.write(Paths.get("outputpath.txt"), new byte[1]);

in file properties:
size: 1 bytes

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.