Write byte[] to file

P

Paul Davis

public void writeFile(byte[] data, String fileName) throws IOException{
OutputStream out = new FileOutputStream(fileName);
out.write(data);
out.close();
}
 
M

M.J. Dance

andrewzzz said:
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!
 
T

Thomas Hawtin

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

Tom Hawtin
 
T

Thomas Hawtin

C

Chris Uppal

M.J. Dance said:
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
 
T

Thomas Fritsch

Chris said:
M.J. Dance said:
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");
}
 
M

Mark Rafn

M.J. Dance said:
byte[] bytes = new byte[file.length()];
in.read(bytes);

Thomas Fritsch said:
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");
}

As long as you're checking for files that got shorter between creating your
byte array and actually reading, you might want to check that it got longer
with in.available() or just by reading another byte and seeing if one's there.

Alternately, you may prefer to just read whatever you can and not trust the
file.length() call at all. This works even if you're not reading from a file
(like from getResourceAsStream() or a network stream).

// best-guess starting size, make it up if we don't have a file.
ByteArrayOutputStream baos = new ByteArrayOutputStream(file.length());
byte[] buf = new byte[1024]; // read up to 1k at a time
boolean done=false;
while (!done) {
int amtRead = in.read(buf);
if (amtRead == -1) {
done = true; // EOF
} else {
baos.write(buf, 0, amtRead);
}
}
byte[] bytes = baos.toByteArray();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top