wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Writing to 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 » I/O and Streams
Bookmark "Writing to File" Watch "Writing to File" New topic
Author

Writing to File

Manoj Zachariah
Ranch Hand

Joined: Dec 08, 2003
Posts: 65
how do i write the contents of an OutputStreamWriter object into a file???
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
check out the java.io package:
there you can find the java.io.FileWriter and java.io.FileOutputstream.
dont forget to close the stream or writer.

karl
Manoj Zachariah
Ranch Hand

Joined: Dec 08, 2003
Posts: 65
can somebody finsih the code for me!!!
it creates a file called temp.txt but doesnt write into it.
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
just add the statement bw.flush(). You should be able to see the o/p in the file.
hope that helps.
Shekar.


Thanks,
Shekar
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
beg u pardon fw.flush()
Shekar.
Manoj Zachariah
Ranch Hand

Joined: Dec 08, 2003
Posts: 65
can you tell me what flush does?
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
when you are using a buffered writer what happens is the data that you are reading from the file gets buffered. The data will be written to the file in two ways. One is by explicitly using the flush which is like pushing the data to the file after getting buffered. the other way is when the buffer gets full it automatically gets flushed. but it is always better to flush the data.
in one line you collect the data and push the data with flush.
hope this helps.
shekar.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the I/O and Streams forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You should get in the habit of calling close() on a Reader/Writer/InputStream/OutputStream when you're done with it. Preferably in a finally{} clause to ensure it happens even if an exception is thrown. This will also call flush() as part of the close() operation, so you don't really need to call flush() separately.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Incidentally, readLine() will return a String that does not include any line terminators (\n or \r). So when you write the record to a new file, you probably want to add new line terminators too. This is typically achieved by using a PrintWriter, which has a println() method which uses your platform's default line terminator.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing to File