aspose file tools
The moose likes Beginning Java and the fly likes writing froma stream to a File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "writing froma stream to a File" Watch "writing froma stream to a File" New topic

writing froma stream to a File

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
I'm Trying to write to a file. what exactly do i need to correct here. Right now it just keeps creating an empty file. Why's that?

[ December 11, 2007: Message edited by: Wolfgang Obi ]
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
Where do you think you are writing something to the file ?

Rob Spoor

Joined: Oct 27, 2005
Posts: 19905

You need to read from the input stream, then copy that to the output stream.

You can do this byte per byte using the read() and write(int) methods, but experience has tought me that using the read(byte[]) and write(byte[], int, int) methods are a lot faster most of the times.

If you're using the byte[] versions, don't forget that read(byte[]) returns the number of bytes actually read. Don't always just go writing the entire array - you'll write garbage at the end of the file.

How To Ask Questions How To Answer Questions
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: writing froma stream to a File