Win a copy of Microservices Testing (Live Project) this week in the Spring 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

PrintWriter please help

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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>
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree 100% with Jim has explained it very nicely
Manish india
javaexams@yahoo.com
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic