aspose file tools*
The moose likes Beginning Java and the fly likes Formatting output when using FileWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Formatting output when using FileWriter" Watch "Formatting output when using FileWriter" New topic
Author

Formatting output when using FileWriter

Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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

Joined: Feb 01, 2006
Posts: 45
Well thank you for clearing that up and I stand corrected
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formatting output when using FileWriter
 
Similar Threads
reading from one file, calculating, writing to another...
how to use jfreechart in the jsp?
MPXJ problem
Compiler Error: cannot find symbol
How to round a float with a 0.25 step