• Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It creates only his.txt file, but String ret is not added to his.txt file. File is blank. Why?

import java.io.*;
public class fil {
static PrintWriter history;
public static void main(String args[]){
try {
history = new PrintWriter(new FileWriter("his.txt"));
//history.print("ret");
} catch(IOException e) {}
history.print("ret");
}
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to flush the stream. Try calling the flush() method in your PrintWriter object.
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul, now it works fine.
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Paul, What is the difference between these two ? both are doing the same thing.

PrintWriter history = new PrintWriter(new FileWriter("his.txt"));
PrintWriter history = new PrintWriter(new FileWriter( new File("his.txt")));
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing really, you are just seeing two different constructors for the FileWriter class.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!