IO Code Example                 Peter Komisar


import java.io.*;

class Streamer{
  public static void main(String[] args){
    if(args.length==0){
      System.out.println("Enter a file name");
      System.exit(0);
      }
  FileOutputStream fos;
  DataOutputStream dos;
  byte[] b=new byte[10];

try{
   fos=new FileOutputStream(args[0]);   // takes the class name from the command line
   dos=new DataOutputStream(fos);    // a dataoutput stream on a file output stream
     for(byte i=0; i<b.length; i++){           // some numbers for the byte array
       b[i]=(byte)i;
       dos.writeByte(b[i]);
       }
    new readFile(args[0]); // constructor for the second class that goes ahead and prints from the first
   }
   catch(IOException io){
   }
 }
}
 

class readFile{
  readFile(String s){
   RandomAccessFile raf;
   byte bX;
   try{
      raf=new RandomAccessFile(s,"rw");
      while( ( bX= raf.readByte( ) ) != -1){
          System.out.print(bX + " ");
          }
      }
      catch(EOFException eof){
        System.out.println ("EOF");}
      catch(IOException io){
        }
  }
}



Question

Change the writeByte method to writeInt and then writeShort then to writeLong
and observe the effects on the program. Explain the results?

// void  writeByte(int v)       Writes out a byte to the underlying output stream as a 1-byte value.