This application illustrates the use of theFileOutputStream
class for writing a stream of data to a file. It can accept two parameters from the commandline: a filename and a path.import java.awt.*; import java.io.*; public class FileStreamRead { // simplest read from a file public FileStreamRead () { super (); } // end FileStreamRead constructor public static void main ( String [ ] args ) { String fileName; String path; File inFile; FileInputStream inStream; String message; char [ ] messageArray = new char [ 100 ]; FileStreamRead readAppl = new FileStreamRead (); if ( args.length > 0 ) fileName= args[0]; else { fileName = new String ( "testfile" ); path = new String ( "/afs/unc/home/jbs/public_html/java/wwwp/lecture_8" ); } if ( args.length > 1 ) path = args[1]; else path = new String ( "/afs/unc/home/jbs/public_html/java/wwwp/lecture_8" ); inFile = new File ( path, fileName ); try { inStream = new FileInputStream ( inFile ); } catch ( IOException except ) { return; } System.out.println ( "Reading from file: " + inFile.getName() ); boolean more = true; int b = -1; while ( more ) { // read whole file int j = 100; for ( int i = 0; i < 100; i++ ) { // read a hunk try {b = inStream.read ( );} catch ( IOException except ) {;} if ( b == -1 ) { more = false; j = i; break; } // end endoffile test messageArray [ i ] = (char)b; i++; } // end of hunk message = new String ( messageArray, 0, j ); System.out.println ( message ); } // end while ( whole file ) try { inStream.close (); } catch ( IOException except ) { ; } } // end main } // end FileStreamRead