• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

writing printwriter output to file

 
Rameshkumar Thirumalaisamy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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

 
Rob Spoor
Sheriff
Pie
Posts: 20753
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Greg Charles
Sheriff
Posts: 3002
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Is that right???

 
Greg Charles
Sheriff
Posts: 3002
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic