Why don't characters in a file get overwritten?

B

beginner16

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
 
L

Lothar Kimmeringer

Hello beginer16,

beginner16 wrote:

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
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

A. Bolmarcich

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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top