Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting output when using FileWriter

 
Brian LaRue
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little background info:

My program uses streams to read data from a file, then performs calculations on what it read, then writes the results to a different output file.

It's great that I even got it working correctly. With that being said, the output is messed up. I need some help on where to go/ what to do to fix it.

Here's what the output of "surcharge.txt" looks like currently:



I would like it to carriage return and line feed after it prints the % of surcharge. For example:

Customer ID: 235678 Surcharge: 12.0%
Customer ID: 698090 Surcharge: 10.0%
.
.
.
and so on...

Any ideas?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One usually doesn't use a "bare" FileWriter; you'll generally "wrap" it in an instance of another Writer subclass that offers some formatting capabilities. For example, PrintWriter:

PrintWriter pw = new PrintWriter(new FileWriter("myfile"), true);
pw.println("Hello on one line");
pw.println("Goodbye on the next line")
pw.close(); // closes the file, too

There are print() and println() methods that accepts Strings, Objects, and every primitive type; the println() ones append a newline, the print() ones don't.
 
Brian LaRue
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hurray!! That is what I wanted! Thanks for the advice, although I noticed a little typo in your suggestion.

I'm guessing that you meant to type:



Thanks again!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian LaRue:

I'm guessing that you meant to type:



No, actually -- my code and your code do two different things. Mine is passing a second boolean argument to the PrintWriter constructor; if you look at the Javadoc, you'll see that means to "autoflush", or update the underlying file each time you call one of the println() methods. Your version passes the boolean to the FileWriter constructor, which , again by looking at the Javadoc, you can see means to open the file in "append mode", as opposed to truncating the file to zero length. Both boolean arguments are valid and useful in their own place, and in fact, the line of code could have a "true" or "false" in both of those places at once, or neither, and still be correct (although of course each version does something a little different.)
 
Brian LaRue
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thank you for clearing that up and I stand corrected
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic