FileInputStream class

 

see InputStream class for descriptions of the methods FileInputStream provides.They are:

 

available( ) ††††††††††††††††† Tells how many bytes can be gotten without blocking.

close( ) ††††††††††††††††††††††† Close the stream.

mark( int readAheadLimit )Mark the present position in the stream.

markSupported( ) ††††††† Tell whether this stream supports the mark(...) operation.

read( )††††††††††††††††††††††† Read a single byte, returning it as the low end of an int.

read( byte[ ] b )†††††††††† Read bytes into a byte array.

read( byte[ ] b, int off, int len)Read bytes into a portion of a byte array.

ready( ) ††††††††††††††††††††††† Tell whether this stream is ready to be read.

reset( )††††††††††††††††††††††† Reset the stream to the beginning.

skip( long n )††††††††††††† Skip characters.

 

 

FileInputStream is a byte InputStream class which actually reads from files. It is usually wrapped by other I/O classes which do things with the the incoming stream which FileInputStream provides.Here it is wrapped:

 

import java.io.*;

 

try {

††† DataInputStream dis = new DataInputStream( new BufferedInputStream( new FileInputStream("c:\\test.txt")));

††† while(dis.available( ) != 0)

††††† System.out.print( (char) dis.readByte( ) );

}

catch (IOException io) { }

 

Its constructors will take any of (1) a File object (used in example below), (2) a FileDescriptor object, or (3) a simple String filepathname.

 

This code reads and prints from a text file:

 

import java.io.*;

 

FileInputStream fis = null;

try{ fis = new FileInputStream(new File("test.txt"));

††††††††††††††† int c;

††††††††††††††† while ((c = fis.read( )) != -1) {

††††††††††††††† System.out.print(String.valueOf( (char)c ));†††† // thereís no toString( ) for byte

††††††††††††††† }

}†††††

catch (IOException ex) { }

finally { try { if (fis != null) fis.close( ); }

††††††††††††† catch (IOException ex) { }

}

 

It adds two methods to InputStream:

 

void finalize( )method

 

Overrides Objectís method to ensure a file Close

 

 

FileDescriptor.getFD( )†† method

 

Returns the associated FileDescriptor object for the file in use