0

I want to write a byte[] data to a file and I found this code:

public static void writeByte(String filename, byte[] data){
 BufferedOutputStream bos = null;
 try
 {
 //create an object of FileOutputStream
 FileOutputStream fos = new FileOutputStream(new File(filename));
 //create an object of BufferedOutputStream
 bos = new BufferedOutputStream(fos);
 /*
 * To write byte array to file use,
 * public void write(byte[] b) method of BufferedOutputStream
 * class.
 */
 System.out.println("Writing byte array to file");
 bos.write(data);
 System.out.println("File written");
 }
 catch(Exception fnfe)
 {
 System.out.println("exception");
 }
 }

This seems to work but I can't open the file to see the data...it says the "file is of an unknown type". Data was written because the file size is 25.5KB. My question is, how can I view the contents? Is there an extension that I have to use? Or do I need a special editor to open it? (Tried geany and gedit...)

asked Nov 21, 2011 at 20:03
1
  • 1
    What data are you writing into the file? What's the filename? How are you opening the file? What gives you the "file is of an unknown type" error? Commented Nov 21, 2011 at 20:06

3 Answers 3

3

Try to flush(); and close(); the stream after writing the bytes to it. If you were trying to write a known file format, this might be problem: that the last buffer of bytes wasn't actually written to the file, which caused the file was "corrupt".

Otherwise, use a hex editor as Richard suggested to see the raw bytes of the file.

answered Nov 21, 2011 at 20:13
Sign up to request clarification or add additional context in comments.

3 Comments

A BufferedOutputStream performs a flush() when you call close(), but it's imperative to call close()!
I know, but I like it. I feels good to write flush();. I think I do it to prevent confusion.
Yep, I do the same; no harm in calling it. I just wanted to emphasize the need to call close(), preferably in a finally (or ARM) block.
1

Use a hex file editor to view the contents. Such as:

These are just the ones I use. They are not necessarily the best hex editors.

answered Nov 21, 2011 at 20:06

1 Comment

Thanks! I was able to view the contents using ghex!
0

You don't need the BufferedOutputStream here at all, but you do need to close whichever stream you're using.

And when you catch an exception, don't just make up your own message. The exception itself contains three vital pieces of information: its class, its message, and its stack trace. Your own message by contrast contains basically one bit of information: it happened. You should always log or print the actual exception class and its message, and in cases of debugging difficulty its stacktrace too.

answered Nov 22, 2011 at 9:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.