Source of BinaryFileOperations.java


  1: import java.io.DataInputStream;
  2: import java.io.DataOutputStream;
  3: import java.io.EOFException;
  4: import java.io.FileInputStream;
  5: import java.io.FileNotFoundException;
  6: import java.io.FileOutputStream;
  7: import java.io.IOException;
  8: import java.util.Random;
  9: /**
 10:    A class of methods that create and display a binary file of
 11:    random integers.
 12:  
 13:    @author Frank M. Carrano
 14:    @author Timothy M. Henry
 15:    @version 5.0
 16: */
 17: public class BinaryFileOperations
 18: {
 19:    /** Writes a given number of random integers to the named binary file.
 20:     @param fileName The file name as a string.
 21:     @param howMany  The positive number of integers to be written.
 22:     @return  An integer code indicating the outcome of the operation:
 23:     0 = Success; > 0 = Error opening (1), writing (2), or closing (3)
 24:     the file. */
 25:    public static int createRandomIntegerFile(String fileName, int howMany)
 26:    {
 27:       int resultCode = 0;
 28:       Random generator = new Random();
 29:       DataOutputStream toFile = null;
 30:       try
 31:       {
 32:          FileOutputStream fos = new FileOutputStream(fileName);
 33:          toFile = new DataOutputStream(fos);
 34:          
 35:          for (int counter = 0; counter < howMany; counter++)
 36:          {
 37:             toFile.writeInt(generator.nextInt());
 38:          } // end for
 39:       }
 40:       catch (FileNotFoundException e)
 41:       {
 42:          resultCode = 1;    // Error opening file
 43:       }
 44:       catch (IOException e)
 45:       {
 46:          resultCode = 2;    // Error writing file
 47:       }
 48:       finally
 49:       {
 50:          try
 51:          {
 52:             if (toFile != null)
 53:                toFile.close();
 54:          }
 55:          catch (IOException e)
 56:          {
 57:             resultCode = 3; // Error closing file
 58:          }
 59:          
 60:          return resultCode;
 61:       }
 62:    } // end createRandomIntegerFile
 63:    
 64:    /** Displays all integers in the named binary file.
 65:        @param fileName  The file name as a string.
 66:        @return  An integer code indicating the outcome of the operation:
 67:                 0 = Success; > 0 = Error opening (1), reading (2), closing (3)
 68:                 the file. */
 69:    public static int displayBinaryFile(String fileName)
 70:    {
 71:       int resultCode = 0;
 72:       DataInputStream fromFile = null;
 73:       try
 74:       {
 75:          FileInputStream fis = new FileInputStream(fileName);
 76:          fromFile = new DataInputStream(fis);
 77:          
 78:          while (true)
 79:          {
 80:             int number = fromFile.readInt();
 81:             System.out.println(number);
 82:          } // end while
 83:       }
 84:       catch (FileNotFoundException e)
 85:       {
 86:          resultCode = 1;    // Error opening file
 87:       }
 88:       catch (EOFException e)
 89:       {
 90:          // Normal occurrence since entire file is read; ignore exception
 91:       }
 92:       catch (IOException e)
 93:       {
 94:          resultCode = 2;    // Error reading file
 95:       }
 96:       finally
 97:       {
 98:          try
 99:          {
100:             if (fromFile != null)
101:                fromFile.close();
102:          }
103:          catch (IOException e)
104:          {
105:             resultCode = 3; // Error closing file
106:          }
107:          
108:          return resultCode;
109:       }
110:    } // end displayBinaryFile
111: } // end BinaryFileOperations