1 Reply Latest reply: Dec 6, 2008 12:34 PM by 843785 RSS

    How to use FileInputStream

    843785
      Hello

      How do i go about displaying the content of a text file in the java cnsole
      from a text file for e.g sitting in C:\WiredNews.txt

      i have some sample code from a book called Java - A Beginner's Guide, 3rd Edition (2005).pdf

      but when i run the code...
      /* Display a text file.
      To use this program, specify the name
      of the file that you want to see.
      For example, to see a file called TEST.TXT,
      use the following command line.
      java ShowFile TEST.TXT
      */
      
      
      package files;
      import java.io.*;
      public class ShowFile {
      
           public static void main(String args[])
           throws IOException
           {
           int i;
           FileInputStream fin;
           try {
           fin = new FileInputStream(args[0]);
           } catch(FileNotFoundException exc) {
           System.out.println("File Not Found");
           return;
           } catch(ArrayIndexOutOfBoundsException exc) {
           System.out.println("Usage: ShowFile File");
           return;
           }
           // read bytes until EOF is encountered
           do {
           i = fin.read();
           if(i != -1) System.out.print((char) i);
           } while(i != -1);
           fin.close();
           }
           }
       
      i get this message in the console.. Usage: ShowFile File

      how does the code find the path to the file..
      any ideas any help will be much appreciated..

      Cheers
        • 1. Re: How to use FileInputStream
          843785
          Here in your programme your FileInputStream class can have the argument of first index of string array i.e.args[0] in its constructor . so when you will not enter anyting during running your class, you will get an outofboundException.