File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes byte[] to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "byte[] to file" Watch "byte[] to file" New topic

byte[] to file

Dave Comeau

Joined: Jun 14, 2002
Posts: 27
I have a byte[] that contains binary image data (for pngs and gifs). I can't figure out how to write this data to a file.
Can anyone give me an overview of what I need to do?
kishor rao
Ranch Hand

Joined: Dec 10, 2002
Posts: 35
FileOutputStream f = new FileOutputStream("pic.gif");
for(int i = 0; i < <size of ur byte array>; i++)
f.write(<ur byte array name>[i]);
i think this should do it.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
No need for a loop - just spit out the whole array at once:

The BufferedOutputStream is optional, but a good idea in general for efficiency.
[ January 30, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Dave Comeau

Joined: Jun 14, 2002
Posts: 27
Worked perfect. Thanks so much!
I agree. Here's the link:
subject: byte[] to file
It's not a secret anymore!