Redirection is an OS Feature

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

Sure, because output.txt is a text file. Once it has been created it is a text file like any other.

Redirection is an OS Feature

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:

C:/temp>dir
 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>

You can send the same characters to a file by doing this:

C:/temp>dir > dirlist.txt

You can type out the contents of the new file by doing this:

C:/temp>type dirlist.txt

The same characters as the first DIR command wrote will be written out. Similar commands work for Unix or for Max OS X.

QUESTION 9:

If there already is a file named stuff.txt, what happens if you do:

C:/temp>java myProgram > stuff.txt