• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

PrintWriter class

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Can anybody tell me the usuage of PrintWriter. I was trying example of K&B. Actually i am bit confused. So Help me out.. Thanks in Advance


Cheers,
Nitin
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The PrintWriter class has functions like print() println() that can be used to write into a file. With a FileWriter you get the conventional read() write() functions
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks john for your reply but the below code doesnt working for me.
according to me it shd write "owdy folks" but it is not.. i m not able to trace the problem. i thought my understanding for printWriter is not correct.

import java.io.*;
class Writer3
{
public static void main(String[] args)
{
try{
File file=new File("testnitintxt.txt");
//FileWriter fw=new FileWriter(file);

PrintWriter pw=new PrintWriter(file);
pw.println("howdy");
pw.println("folks");
pw.write("howdy");
pw.write("folks");
}
catch(IOException e){}
}
}


Regards,
nitn
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always flush the toilet

 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks john . i got u.

but another stupid question flush() is to free the resources but without using flush it shd write the content to text file isn't it? please clarify and thanks alot for ur prompt reply.


regards,
nitin
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes it should. Well some times it does and some times it does not. I remember another thread quite sometime back in the "java in general intermediate" forum. A program read around 80k lines but it only wrote 79.8k lines back into a file because the stream was not flushed. Just remember that before the stream is closed you should always flush the buffer.
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot john, you are wonderful. you flushed my doubts.

Regards,
nitin
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we close the stream, doesn't it automatically flush it before closing?

regards,
vijay.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the stream would have done the flushing we wouldn't have 'flush method' in IO classes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic