Sure, because output.txt
is a text file.
Once it has been created it is like any other text file .
File redirection is a feature of the operating system's command line interface, not a feature of Java. File redirection can be used with any program that writes to the monitor.
For example, the DOS command DIR writes a summary of the files in a directory to the monitor. You can send the DIR output to a file by doing this:C:\temp>dir > fileList.txt
Then type out the contents of the new file by doing this:
C:\temp>type fileList.txt
Similar commands work for Unix, Linux, or for Max OS.
(Use cat
instead of type
, and
use ls
instead of dir
.
Compatibility Note: Output redirection is a feature of the operating system, not of the Java system. It works for the older command line of Windows and for Windows PowerShell. It also works for the command line of current and older versions of Linux and Unix.
C:\temp>dir > fileList.txt C:\temp>type fileList.txt Volume in drive C is DISK4_VOL1 Volume Serial Number is 1C25-5670 Directory of C:\temp 01/09/2006 07:29p <DIR> . 01/09/2006 07:29p <DIR> .. 01/09/2006 07:29p 115 Hello.java 1 File(s) 115 bytes 2 Dir(s) 30,987,882,496 bytes free C:\temp>
If there already is a file named stuff.txt, what happens if you do:
C:\temp>java myProgram > stuff.txt