1   //  File:  FileIO\FileIO.java

  2   import java.io.*;

  3   /**
  4    * Show how to do simple stream-oriented input/output in Java.<br>
  5    * Show how to use StreamTokenizer to parse input.<br>
  6    * Show how to read line-at-a-time.
  7    * 
  8    * Note: Java 1.5's Scanner class may be helpful...
  9    *
 10    * @author Bary W Pollack
 11    * @version version 02/25/01 - 17:00 - Modified
 12    */

 13   public class FileIO {

 14       /**
 15        * Name of the input file from which data is to be read.
 16        */
 17       static final String sInFile  = "input.txt";

 18       /**
 19        * Name of the output file to which data is to be written.
 20        */
 21       static final String sOutFile = "output.txt";

 22       /**
 23        * Show how to set up a StreamTokenizer in order to read from
 24        * an input stream and properly parse the data.<br>
 25        * Show how to do simple stream-oriented I/O to and from files.<br>
 26        * Show how to read from a file one line at a time.
 27        */

 28       FileIO() {
 29           try {
 30               // ===========================================================
 31               // First, demonstrate simple stream input/output...
 32               // Read until EOF...
 33               // ===========================================================

 34               // set up the input in two steps because we want to be able to
 35               // invoke the reader's close() method later on...
 36               FileReader reader = new FileReader(sInFile);
 37               StreamTokenizer tokens = new StreamTokenizer(
 38                                            new BufferedReader(reader));
 39               // tell the tokenizer that we want to parse numbers "properly"
 40               tokens.parseNumbers();
 41               tokens.eolIsSignificant(true);
 42               tokens.ordinaryChar('/');

 43               // set up the output writer
 44               PrintWriter outs = new PrintWriter(
 45                                      new BufferedWriter(
 46                                          new FileWriter(sOutFile)), true);

 47               System.out.println("TokenType  _______Value_______\n");
 48               outs.println("TokenType  _______Value_______");
 49               outs.println();

 50               int next = 0;
 51               while ((next = tokens.nextToken())!= StreamTokenizer.TT_EOF) {
 52                   switch (next) {
 53                       case StreamTokenizer.TT_WORD:
 54                           System.out.println("WORD:      " + tokens.sval);
 55                           outs.println("WORD:      " + tokens.sval);
 56                           break;
 57                       // Note how scientific notation is (not) handled!
 58                       case StreamTokenizer.TT_NUMBER:
 59                           System.out.println("NUMBER:    " + tokens.nval);
 60                           outs.println("NUMBER:    " + tokens.nval);
 61                           break;
 62                       case StreamTokenizer.TT_EOL:
 63                           System.out.println("EOL");
 64                           outs.println("EOL");
 65                           break;
 66                       default:
 67                           System.out.println("OTHER:     " + (char) next);
 68                           outs.println("OTHER:     " + (char) next);
 69                           break;
 70                   }
 71               }

 72               reader.close();
 73               outs.close();

 74               // ===========================================================
 75               // Now, demonstrate line-oriented input...
 76               // ===========================================================

 77               System.out.println("\n\nBegin echoing input...\n");
 78               BufferedReader line = new BufferedReader(
 79                                         new FileReader(sInFile));
 80               String s;
 81               while ((s = line.readLine()) != null)
 82                   System.out.println("<" + s + ">");
 83               line.close();

 84           } catch(EOFException e) {
 85               System.out.println("\nEnd of File encountered\n\n" + e);

 86           } catch(FileNotFoundException e) {
 87               System.out.println("\nFile Not Found encountered\n\n" + e);

 88           } catch(IOException e) {
 89               System.out.println("\nIO exception encountered\n\n" + e);
 90           }
 91       }

 92       /**
 93        * Runs FileIO()...  There should be a pre-existing file, called
 94        * input.txt, in the current directory.  An output file,
 95        * output.txt, will be created in the current directory as a
 96        * result of executing the program.<br>
 97        * This program reads from input.txt, parsing the input as it
 98        * goes, and displays the items that it reads both on stdout
 99        * as well as writing a copy of this information in output.txt.<br>
100        * Then the program reads from input.txt line-at-a-time,
101        * echoing the lines to stdout.
102        *
103        * @param args unused
104        */

105       static public void main(String args[]) {
106           System.out.println("***** FileIO Demo ***** \n\n"
107                            + "Reading and writing files...\n");

108           new FileIO();

109           System.out.println("\nFini... \n");
110       }
111   }

112   /*  Screen output from running this program:

113   ***** FileIO Demo ***** 

114   Reading and writing files...

115   TokenType  _______Value_______

116   WORD:      aStringOfCharacters
117   EOL
118   WORD:      a
119   WORD:      character
120   EOL
121   EOL
122   NUMBER:    -9.0
123   NUMBER:    12345.0
124   NUMBER:    3.14159
125   NUMBER:    13.57
126   WORD:      E-24
127   EOL
128   WORD:      somePunctuation
129   OTHER:     <
130   OTHER:     ,
131   OTHER:     :
132   OTHER:     ;
133   OTHER:     #
134   OTHER:     ~
135   OTHER:     !
136   OTHER:     (
137   OTHER:     @
138   OTHER:     )
139   OTHER:     *
140   OTHER:     ^
141   OTHER:     /
142   OTHER:     +
143   OTHER:     -
144   OTHER:     =
145   EOL
146   WORD:      lastLineOfInput

147   Begin echoing input...

148   <aStringOfCharacters>
149   <a       character>
150   <>
151   <-9 12345 3.14159 13.57E-24>
152   <somePunctuation<,:;#~!(@)*^/+-=>
153   <lastLineOfInput>

154   Fini... 

155   ..... */

The INPUT in ‘input.txt’ was:
AStringOfCharacters a       character -9 12345 3.14159 13.57E-24  somePunctuation<,:;#~!(@)*^/+-= lastLineOfInput