• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

writing printwriter output to file

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to write the printwriter output in the code below to a file.I am new to java.
Please help me thanks in advance

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

A PrintWriter is little more than a wrapper around any other Writer. This can be a FileWriter if you need to write to a file. Just check out the PrintWriter constructors.
 
Rameshkumar Thirumalaisamy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you help me with a piece of code which i can insert into my code .

I am very new to programming .I find it difficult to understand
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rameshkumar,

We try not to just post code solutions for beginning programmers, since it takes away both the joy and the effectiveness of learning. However, I can give you a couple of more hints:

- Do you know how to use a FileWriter to write data to a file. Show us how you'd do that.
- Rob's link to PrintWriter constructors will show you how to wrap a FileWriter so it acts as a PrintWriter, but still writes to a file.
- You will create your FileWriter wrapped in a PrintWriter, then pass that as a parameter to formatTx().

So give that a try, and post your new code. We'll help you polish it up. Oh, and when you post that new code, just hit the "Code" button at the top of the editor. That will make your code much easier to read. Thanks! (I've added code tags to your original post.)
 
Rameshkumar Thirumalaisamy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that right???

 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks good, but you're redefining the "out" parameter that you pass into formatTx(). I think the idea is to setup your PrintWriter first, then call formatTx() as it was written in your original example.

Something like:



Also keep in mind that streams that are opened must be closed. So always use something like:



Not closing file streams, sockets, and database connections is an easy mistake to make, but it can cause your program to slowly grind to a halt after seeming to work fine for hours or even days.
reply
    Bookmark Topic Watch Topic
  • New Topic