This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 3432
    
  12
Where do you think you are writing something to the file ?


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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
 
Similar Threads
Client
Pulling file via HTTP URL - Uses Authentication
Servlets problem
File Download Functionality in jsf
Downloading File From URL