Accessing Files

Java opens files by declaring a File Input Stream. The stream is then set as a parameter to a buffered input stream. An array of bytes is declared to hold the data and while the buffered stream is not empty data is read into the array. Any errors are captured to an IO exception handler.

 

Copy the following code into a text editor and
save it as fileview.java.

import java.io.*;
public class fileview {
 public static void main (String args[]){
  try
    {
     FileInputStream in = 
       new FileInputStream(args[0]);
     BufferedInputStream bufin = 
       new BufferedInputStream(in);

     byte bitearray[] = new byte[256];
     int numbytesread;

     while (bufin.available()>0)
      {
        numbytesread = bufin.read(bitearray);
        System.out.println
       ("Number of bytes read = " + numbytesread);
        String s = new String(bitearray,0);
        System.out.println(s);
      }
   }
   catch(IOException ioe)
   {
        System.out.println(ioe.toString());
   }
  }
}

        


The 'try' method will attempt to open the
file input stream and send any errors to
the IO exception handling routine.

We attempt to open the file with the name
given in the first command line argument
args[0] and declare a buffered input stream
to hold that file's data

We need an array to hold the bytes and
a counter.

While we haven't covered every element
in the file.

Input an array of bytes and

print them out




Notice that we need a separate
'catch' routine to handle the IO errors.

 

After compiling this program run it by typing

java fileview <parameter- (name of the file you want to view)>

This program needs a name as a command line parameter. It will then search for the file and print out all data in the file. Using this program you can open and display any text file available on your machine. For example, if you were to place the following file on your drive C: in the Java directory named TEST.TXT

This is a test of the Java data stream system.
Yes you will be able to use this to open files
on your local drives or ,with permissions from the owner, over the Internet!

To view the above file one would enter
java fileview C:\Java\TEST.TXT

The output should be identical to the data entered with the possibility of some problems with the commas on any MS DOS systems.

To write files under Java we use the same stream methods. For example to write a new file with the name given as the first parameter in the argument list we would enter.

FileOutputStream out = new FileOutputStream(args[0]);
BufferedOutputStream bufout = new BufferedOutputStream(out);

The file is created by the first line. Now we need to declare an array of bytes and send those arrays to the bufout stream.

out.write(<output byte array name>);

And then finally we should use the 'close' method to close the file.

Congratulations! You have completed our section on Java applications.
As you have seen, Java has the same facilities we find in other languages but we must use an interpreter to actually run the applications. Yes, it is slower, but the exact same code will run on any machine and we can incorporate that interpreter into web browser software. That means Java class files can be automatically downloaded with any web page and run. Unfortunately there are some interface problems. These are handled by writing our Java code as extensions of the Java Applet class. Applets contain all the facilities we need to enable our code to run on a browser.

Go to the next topic: Setting up an HTML Page

Back to the Table of Contents