Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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!
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic