Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply

Java - Write byte[] to file

 
Thread Tools Search this Thread
Old 11-20-2006, 12:04 PM   #1
andrewzzz
 
Posts: n/a
Default Write byte[] to file

hi guys,
what is the best way to write a byte array to a file?
thanks a lot,bye

  Reply With Quote
Old 11-20-2006, 12:45 PM   #2
Paul Davis
 
Posts: n/a
Default Re: 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


  Reply With Quote
Old 11-20-2006, 12:51 PM   #3
M.J. Dance
 
Posts: n/a
Default Re: 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.just****inggoogleit.com/s...le+io+tutorial

Ta ta!
  Reply With Quote
Old 11-20-2006, 12:52 PM   #4
Thomas Hawtin
 
Posts: n/a
Default Re: 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
  Reply With Quote
Old 11-20-2006, 01:31 PM   #5
Thomas Hawtin
 
Posts: n/a
Default Re: 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.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
Old 11-20-2006, 04:46 PM   #6
Chris Uppal
 
Posts: n/a
Default Re: 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



  Reply With Quote
Old 11-20-2006, 05:30 PM   #7
Thomas Fritsch
 
Posts: n/a
Default Re: 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
  Reply With Quote
Old 11-20-2006, 06:43 PM   #8
Mark Rafn
 
Posts: n/a
Default Re: Write byte[] to file

>> 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 Search this Thread
Search this Thread:

Advanced Search

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

vB 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
SONY DVD RW DW-G120A SOMETIMES FAILS...... atlantic965 DVD Video 0 06-18-2006 09:36 PM
problems backing up dvds Lawrence Traub DVD Video 11 09-27-2005 06:34 PM
Re: Ripping DVDs. Please answer the attached question. - Question.txt Stan Brown DVD Video 19 02-09-2005 10:19 PM
Burn process failed - help! Log file posted for help troubleshooting Michael Mason DVD Video 1 08-16-2004 08:24 PM
Pioneer A05 Problems Bill Stock DVD Video 8 11-28-2003 04:03 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

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