This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes PrintWriter please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "PrintWriter please help" Watch "PrintWriter please help" New topic

PrintWriter please help

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
import java.util.*;
class rd2
public static void main(String a []) throws IOException
Vector v = new Vector();
FileReader fr1 = new FileReader("c:\\read.txt");
Filewriter fw1 = new FileWriter("c:\\out.txt");
BufferedReader br1 = new BufferedReader(fr1);
PrintWriter pw1 = new PrintWriter(fw1);
String str ;
while ((str = b1.readLine()) ! = null)
for (int i = (v.size() -1) ; i > -1 ; --i)
String str1 = (String) v.get(i);
pw1.println(str1); // I guess here is the problem. It is not printing to file. But last modified TimeStamp is changed.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
The stream needs to be flushed. (Often true of output streams in general.) This can be done several ways:
  • When done writing, call flush() method on outermost stream - PrintWriter in this case. It will in turn recursively flush any inner streams - FileWriter in this case.
  • When done writing, call close() method on outermost stream (PrintWriter), which will in turn call flush() for you.
  • When you first create the PrintWriter, use the constructor new PrintWriter(fw1, true). This sets "autoflush" mode to "on", and you no longer have to worry about it. Unfortunately it only works for PrintWriters, so you need to know the other methods as well.

"I'm not back." - Bill Harding, Twister
Manish Joshi

Joined: Oct 16, 2000
Posts: 16
I agree 100% with Jim has explained it very nicely
Manish india
It is sorta covered in the JavaRanch Style Guide.
subject: PrintWriter please help
It's not a secret anymore!