FileDocCategorySizeDatePackage
FileInputReader.javaAPI DocExample2547Tue Nov 04 09:23:24 GMT 1997None

FileInputReader

public class FileInputReader extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

  System.out.println( "This program will read, and echo the tokens from a file\n" );

      // First, get a file name for the user
      BufferedReader is = new BufferedReader(
                                new InputStreamReader( System.in ) );
      String filename = null;
      BufferedReader fis = null;

      System.out.print( "Please enter a the file name : " );
      while( true )
      {
         try
         {  filename = is.readLine();
         }
         catch( IOException ioe )
         {  ioe.printStackTrace();
            System.exit( -1 );
         }

         // Now to open the external file
         try
         {  fis = new BufferedReader( new FileReader( filename ) );
            break;
         }
         catch( FileNotFoundException fnf )
         {  System.out.print( "File not found, try again : " );
            continue;
         }
      }
      System.out.println( "File " + filename + " opened successfully." );


      // Now process the tokens on a line-by-line basis
      System.out.print( "\nListing tokens : \n" );

      String instring = null;
      StringTokenizer st = null;
      String item = null;
      int lineNo = 0;

      mainloop:
      while( true )
      {  try
         {  instring = fis.readLine();
            if( instring == null ) // eof reached
            {  break mainloop;
            }
            lineNo++;
            System.out.println( "Line #" + lineNo + " : " + instring);
         }
         catch( IOException ioe )
         {  ioe.printStackTrace();
            break mainloop;
         }

         // Got a line OK, so take it to pieces:
         st = new StringTokenizer( instring );
         while( st.hasMoreTokens() )
         {  try
            {  item = st.nextToken();
               System.out.println( "\t" + item );
            }
            catch( NoSuchElementException nse )
            {  // Shouldn't be thrown but ...
               nse.printStackTrace();
               continue mainloop;
            }
         }
      } // end while

      try
      {  fis.close();
      }
      catch( IOException ioe )
      {  ioe.printStackTrace();
         // no other action this time - it is the end anyway
      }
            
      System.out.println( "\n*** End of File ***\n" );