aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about File, FileWriter + BufferedWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about File, FileWriter + BufferedWriter" Watch "Question about File, FileWriter + BufferedWriter" New topic
Author

Question about File, FileWriter + BufferedWriter

Florin Florentin
Ranch Hand

Joined: May 12, 2009
Posts: 43
Hello.

I have a question about working and writing to files. I tried to write to a file using a BufferedWriter, and then use the same BufferedWriter to write to a new file. I've created a new FileWriter object, in which constructor i've passed a new file reference, but that not worked as i was expecting.

Here is the code :



After i'm running this code, there is no text been writed in f2. It's been writed twice in f1.
If i insert this
bw = new BufferedWriter(fw);
on the "//insert code" line, the code will run as i want, but i don't understant why it's not enough to make only fw refering to a new object ?

Is this a String related behavior?
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Florin Florentin wrote:Hello.

I have a question about working and writing to files. I tried to write to a file using a BufferedWriter, and then use the same BufferedWriter to write to a new file. I've created a new FileWriter object, in which constructor i've passed a new file reference, but that not worked as i was expecting.

Here is the code :



After i'm running this code, there is no text been writed in f2. It's been writed twice in f1.
If i insert this
bw = new BufferedWriter(fw);
on the "//insert code" line, the code will run as i want, but i don't understant why it's not enough to make only fw refering to a new object ?

Is this a String related behavior?


Just before the // insert code, "bw" is still pointing to the old FileWriter(f1) even though you have re assigned fw= new FileWriter(2);
That means anything you write on "bw" writes to the old file f1.

This is why you have to re assign bw by inserting the "// bw = new BufferedWriter(fw)" to get the text written to File2


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Excellent clarity of response Rajeev.

Good luck mate,

Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Prithvi Sehgal wrote:Excellent clarity of response Rajeev.

Good luck mate,


thanks Prithvi, good luck to you too
 
 
subject: Question about File, FileWriter + BufferedWriter