This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates 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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "PrintWriter please help" Watch "PrintWriter please help" New topic
Author

PrintWriter please help

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
<html>
<body>
<code>
import java.io.*;
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)
{
v.addElement(str);
}
System.out.println(v.size());
for (int i = (v.size() -1) ; i > -1 ; --i)
{
String str1 = (String) v.get(i);
System.out.println(str1);
pw1.println(str1); // I guess here is the problem. It is not printing to file. But last modified TimeStamp is changed.
}
}
</code>
</body>
</html>
Jim Yingst
Wanderer
Sheriff

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
Greenhorn

Joined: Oct 16, 2000
Posts: 16
I agree 100% with Jim has explained it very nicely
Manish india
javaexams@yahoo.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PrintWriter please help