Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic