• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Two PrintWriter problem

 
Ranch Hand
Posts: 41
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider the following code...

it outputs bazinga ! why?
 
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sahil Manchanda
Ranch Hand
Posts: 41
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 .
 
Greenhorn
Posts: 18
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
* @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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic