• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing to File

 
Manoj Zachariah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i write the contents of an OutputStreamWriter object into a file???
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can somebody finsih the code for me!!!
it creates a file called temp.txt but doesnt write into it.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just add the statement bw.flush(). You should be able to see the o/p in the file.
hope that helps.
Shekar.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
beg u pardon fw.flush()
Shekar.
 
Manoj Zachariah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you tell me what flush does?
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic