wood burning stoves 2.0*
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "writing froma stream to a File" Watch "writing froma stream to a File" New topic
Author

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
Rancher

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


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: writing froma stream to a File