Write byte[] to file  
Author Message
andrewzzz





PostPosted: 2006-11-20 21:04:00 Top

java-programmer, Write byte[] to file hi guys,
what is the best way to write a byte array to a file?
thanks a lot,bye

 
Paul Davis





PostPosted: 2006-11-20 21:45:00 Top

java-programmer >> Write byte[] to file public void writeFile(byte[] data, String fileName) throws IOException{
OutputStream out = new FileOutputStream(fileName);
out.write(data);
out.close();
}
andrewzzz wrote:
> hi guys,
> what is the best way to write a byte array to a file?
> thanks a lot,bye

 
M.J. Dance





PostPosted: 2006-11-20 21:51:00 Top

java-programmer >> Write byte[] to file andrewzzz wrote:
> hi guys,
> what is the best way to write a byte array to a file?

This question screams for a how-to-make-a-rat-pie recipe type of an answer. So,
without further ado...

1. First you have to obtain those bytes. The best way is to read them from a file:

File file = new File("afile");
InputStream in = new FileInputStream(file);
byte[] bytes = new byte[file.length()];
in.read(bytes);
in.close();

//Exception handling is left to a gentle reader.

2. Now write them to a file.

Simple, heh?


For other not-so-best ways you can also resort to

http://java.sun.com/docs/books/tutorial/essential/io/

or, if the going gets extremely tough,

http://www.justfuckinggoogleit.com/search?q=java+file+io+tutorial

Ta ta!
 
 
Thomas Hawtin





PostPosted: 2006-11-20 21:52:00 Top

java-programmer >> Write byte[] to file Paul Davis wrote:
> public void writeFile(byte[] data, String fileName) throws IOException{
> OutputStream out = new FileOutputStream(fileName);
try {
> out.write(data);
} finally {
> out.close();
}
> }
> andrewzzz wrote:
>> hi guys,
>> what is the best way to write a byte array to a file?
>> thanks a lot,bye

Tom Hawtin
 
 
Thomas Hawtin





PostPosted: 2006-11-20 22:31:00 Top

java-programmer >> Write byte[] to file M.J. Dance wrote:
> InputStream in = new FileInputStream(file);
> byte[] bytes = new byte[file.length()];
> in.read(bytes);
> in.close();

Are you sure you don't want to check the return value of read, or use
another method from a decorator?

> or, if the going gets extremely tough,
>
> http://www.justfuckinggoogleit.com/search?q=java+file+io+tutorial

There's a lot of really bad advice on the web. Er, and on Usenet. Oh and
in books too.

Tom Hawtin
 
 
Chris Uppal





PostPosted: 2006-11-21 1:47:00 Top

java-programmer >> Write byte[] to file M.J. Dance wrote:

> byte[] bytes = new byte[file.length()];
> in.read(bytes);

/NEVER/ do that. Never !

(There are other problems with the code too, but that one is unforgivable, even
for throwaway code).

-- chris



 
 
Thomas Fritsch





PostPosted: 2006-11-21 2:31:00 Top

java-programmer >> Write byte[] to file Chris Uppal wrote:
> M.J. Dance wrote:
>
>>byte[] bytes = new byte[file.length()];
>>in.read(bytes);
>
> /NEVER/ do that. Never !

It should be replaced by something like this:

byte[] bytes = new byte[file.length()];
for (int n = 0, x; n < bytes.length; n += x ) {
x = in.read(bytes, n, bytes.length - n);
if (x < 0)
throw new EOFException("stream shorter than expected");
}

--
Thomas