Win a copy of The Java Performance Companion this week in the Performance forum!
    Bookmark Topic Watch Topic
  • New Topic

Writing my program to a text file.

 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
The easiest way to do it, in my opinion, is to use the shell to re-direct your output:

java HeatIndex > filename.txt
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
ok. How would I use that in my program. I don't really understand what "java HeatIndex > filename.txt " means.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
ok. Thanks. I think I almost got it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks. I think I almost got it
 
    Bookmark Topic Watch Topic
  • New Topic