File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Writing my program to a text file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Writing my program to a text file.

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
Can somebody please help me with this code? I have the program correctly coded and operating properly, however, I forget how to write my output to a text file. Can somebody please help me here? Thanks.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11155
    
  16

The easiest way to do it, in my opinion, is to use the shell to re-direct your output:

java HeatIndex > filename.txt


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
If you have got a Scanner, you can use its opposite, which is called Formatter, and use its format method.
You need to find out about the % tags first; there are some simple examples in the Java Tutorials and fuller details in the API for Formatter.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
I see you have got the % tags working: that's half the problem solved, and Fred's suggestion looks really nice.
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
ok. How would I use that in my program. I don't really understand what "java HeatIndex > filename.txt " means.
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

what fred rosenberger actually means is to use the command line to write the output to the file when you run the class

>>Java classname > output_filename.txt

-kolli
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
ok. Thanks. I think I almost got it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11155
    
  16

to run your program, you probably type something like this:

java HeatIndex

Your program writes everything to what's called 'Standard out', which usually means the console. Most operating systems let you 'redirect' standard out to some other 'thing' - a printer, a file, a port maybe...

to do that, you tell the OS "hey, when you run this program, redirect the standard out to this other place". That's what the '>' does. Think of it like an arrow, pointing to where you want your output to go. If you want it to go to a file called "output.txt", you'd write

java HeatIndex > output.txt

Note that you can put a full path here:

java HeatIndex > C:\logs\heatindex\outout.txt

Each time you do this, any existing file with that name will be blown away, and ONLY the output from the new run will be saved there. If you want to APPEND your new data to the end of the file (or create it if it doesn't exist), you can do this:

java HeatIndex >> output.txt
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
Thanks. I think I almost got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing my program to a text file.
 
Similar Threads
Somebody please take a look at this
Double doubt
Can somebody help me understand this problem?
Calculating a number in a category
HashSet Magic