The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Ans : Read This....

Posted by BMVarun on May 25, 2001 at 8:34 AM

Hi,

Basically when you use these I/O classes the data you pass stays in the memory buffer of these Streams and you need to flush oc close it to finally write the data to the file.

This is because the Disk I/O takes a lot of time so instead of every time going for Disk I/O these Streams hold the Data till it is enough to write. And when you close it it has to write no matter how much data it holds.

Enjoy !!

From
BMVarun


> class filereaderdemo{
> public static void main(String[] arg)throws Exception{
> FileReader fin=new FileReader("file.txt");
> FileWriter fout=new FileWriter("file1.txt);
> int c;
>
> while((c=read())!=-1)
> {
> fout.write(c);
> }
> /*fout.close(); if this statement is commented the file is not written*/
> }
> }
>
> When i use filewriter and filereader and if i dont close the filewriter object the file is not writtin,why is this?
> When i use FileInputStream and FileOutputStream and if i dont close the FileOutputStream object the file is still copied,why is that ? why should i close the writer object ?






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us