File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Two PrintWriter problem 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 "Two PrintWriter problem" Watch "Two PrintWriter problem" New topic
Author

Two PrintWriter problem

Sahil Manchanda
Ranch Hand

Joined: Jul 24, 2012
Posts: 41

consider the following code...

it outputs bazinga ! why?
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

You have two objects that are pointed to the same file. Each one appends a line to that file every time you println to it. I would expect the contents of your file to be

bazinga
we are even
yes i did

What did you expect to see and why?


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Sahil Manchanda
Ranch Hand

Joined: Jul 24, 2012
Posts: 41

Tina Smith wrote:You have two objects that are pointed to the same file. Each one appends a line to that file every time you println to it. I would expect the contents of your file to be

bazinga
we are even
yes i did

What did you expect to see and why?

actually when two writers are associated with a single file,
the second writer to be flushed overrites the first writer .
Deepak Kumar Jha
Greenhorn

Joined: Feb 07, 2013
Posts: 17

import java.io.*;
public class IO{
public static void main(String args[]) throws IOException{
File f = new File("doom.txt");
PrintWriter k = new PrintWriter(f);
PrintWriter p = new PrintWriter(f);
p.println("bazinga ");
k.println(" we are even ");
p.write("yes i did");
p.close();
k.close();
BufferedReader fr = new BufferedReader(new FileReader(f));
System.out.print(fr.readLine());
}
}

case 1: In this problem when the sequence of line 5 & 6 changed then the output become
output:
we are even

But if we remove ln from println at line 8 thenoutput become
output:
we are even i did

when we remove ln from line 7 & 8 then output become
output:
we are even did

as @Tina Smith said when two writers are associated with a single file,
the second writer to be flushed overwrites the first writer. so in this case the file doom.txt should contain "we are even" i.e true for case 1.
but when we are removing ln from println then both object comes into picture so file contain changed , and the content is not understandable by me . what actually happening with this sort of code can anyone make me understand.
Deepak Kumar Jha
Greenhorn

Joined: Feb 07, 2013
Posts: 17

* @Sahil Manchanda "when two writers are associated with a single file,
the second writer to be flushed overwrites the first writer. "

correct it i wrote @tina smith which was wrong in my previous post.sorry!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two PrintWriter problem
 
Similar Threads
readLine() and read() functionality
Mock Question Doubt...EQ+
Heap size increases when writing XML to the File
Use of File Class...
Does BufferedWriter throwan IOexception?