Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Write byte[] to file

Reply
Thread Tools

Write byte[] to file

 
 
andrewzzz
Guest
Posts: n/a
 
      11-20-2006
hi guys,
what is the best way to write a byte array to a file?
thanks a lot,bye

 
Reply With Quote
 
 
 
 
Paul Davis
Guest
Posts: n/a
 
      11-20-2006
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


 
Reply With Quote
 
 
 
 
M.J. Dance
Guest
Posts: n/a
 
      11-20-2006
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.just****inggoogleit.com/s...le+io+tutorial

Ta ta!
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      11-20-2006
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
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      11-20-2006
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.just****inggoogleit.com/s...le+io+tutorial


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

Tom Hawtin
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      11-20-2006
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



 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      11-20-2006
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
 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      11-20-2006
>> M.J. Dance wrote:
>>>byte[] bytes = new byte[file.length()];
>>>in.read(bytes);


>Chris Uppal wrote:
>> /NEVER/ do that. Never !


Thomas Fritsch <> wrote:
>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();
--
Mark Rafn <http://www.dagon.net/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Write int as a 4 byte big-endian to file. MS C Programming 43 03-13-2012 10:04 PM
write byte array to file Rajesh Olafson Ruby 15 12-22-2010 10:31 PM
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
how to read/write a characters stream which is either of one byte/2 byte Deep C Programming 6 02-28-2007 12:03 PM
read/write data byte-per-byte to and from a socket crash.test.dummy Java 1 02-17-2006 05:18 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57