Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Why don't characters in a file get overwritten?

Thread view: 
beginner16 - 01 Aug 2007 20:16 GMT
hello

1)

FileOutputStream     f   = new FileOutputStream     ( "E:/A.txt" );
OutputStreamWriter f1 = new OutputStreamWriter ( f, "unicode" );
BufferedWriter         f2 =new  BufferedWriter          ( f1 );
f2.write ( "seconD" );
f1.write ( "first" );
f2.close ();

a) After the above code runs, the file has words "firstseconD"
written. Why didn't code f1.write("first") overwrite characters inside
"seconD" string --> thus file would have  "firstD" written in it?

b) If file A.txt existed prior to the following code, it gets deleted.
How do we prevent that?

FileOutputStream f  = new FileOutputStream ( "E:/A.txt" );

2) When I write to a file and if I don't use flush() method, data
doesn't get written. I assumed that even if one doesn't use flush()
method, data eventually gets written to a file, by eventually I mean
before the program ends. But it seems that is not the case?!

thank you
Lothar Kimmeringer - 01 Aug 2007 20:55 GMT
Hello beginer16,

this is the second post of you with a list of questions.
All these questions show that you are not aware of the
fact, that there is a quite good documentation called
Javadocs that are answering more or less everything you
are asking here.

> a) After the above code runs, the file has words "firstseconD"
> written. Why didn't code f1.write("first") overwrite characters inside
> "seconD" string

If you want to do this you need to open the file as
Random Access File. The behavior of a "stream" here
is as it is defined.

> b) If file A.txt existed prior to the following code, it gets deleted.
> How do we prevent that?

Look into the Javadoc of FileOutputStream (hint: there is
more than one constructor).

> 2) When I write to a file and if I don't use flush() method, data
> doesn't get written. I assumed that even if one doesn't use flush()
> method, data eventually gets written to a file, by eventually I mean
> before the program ends. But it seems that is not the case?!

You can't assume that. Look into the Javadocs. flush() is only
be called implicitly if the close-method is called. The behavior
of the output-stream during finalization is undefined.

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

A. Bolmarcich - 01 Aug 2007 21:25 GMT
> 1)
>
[quoted text clipped - 8 lines]
> written. Why didn't code f1.write("first") overwrite characters inside
> "seconD" string --> thus file would have  "firstD" written in it?

Why should it overwrite the characters?

f2.write("seconD") writes its argument value to the buffer in the
BufferedWriter object.  f1.write("first") writes its argument value
to the OutputStreamWriter which converts the characters of the
argument to a stream of bytes and writes that stream of bytes to the
FileOutputStream.  f2.close()

- flushs the buffer in the BufferedWriter object to its underlying
 OuputStreamWriter which converts the character in the buffer to a
 stream of bytes and writes that stream of bytes to the FileOutputStream

- closes its underlying OutputStreamWriter which closes the
 FileOutputStream

> b) If file A.txt existed prior to the following code, it gets deleted.
> How do we prevent that?
>
> FileOutputStream f  = new FileOutputStream ( "E:/A.txt" );

What do you want to do if the file existed prior to statement?

If you want to leave the file unchanged create a File object for the
filename, test whether the file exits, and if it does, don't write
to it.

If you want to add onto the end of the existing file use a

 FileOutputStream f  = new FileOutputStream ( "E:/A.txt", true );

> 2) When I write to a file and if I don't use flush() method, data
> doesn't get written. I assumed that even if one doesn't use flush()
> method, data eventually gets written to a file, by eventually I mean
> before the program ends. But it seems that is not the case?!

Why did you assume that?  An object that buffers data flushes its
buffer when 1) it feels like it, such as when the buffer is full,
and 2) when it is told to, such as when its flush method is invoked.
Otherwise, the data stays in the buffer.

flush may be invoked directly by your own code or indirectly by a
method that you invoke, such as close.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.