Source of Doubler.java


  1: 
  2: import java.io.*;
  3: import java.util.*;
  4: 
  5: public class Doubler
  6: {
  7:     private ObjectInputStream inputStream = null;
  8:     private ObjectOutputStream outputStream = null;
  9: 
 10:     /**
 11:      Doubles the integers in one file and puts them in another file.
 12:     */
 13:     public static void main(String[] args)
 14:     {
 15:         Doubler twoTimer = new Doubler( );
 16:         twoTimer.connectToInputFile( );
 17:         twoTimer.connectToOutputFile( );
 18:         twoTimer.timesTwo( );
 19:         twoTimer.closeFiles( );
 20:         System.out.println("Numbers from input file");
 21:         System.out.println("doubled and copied to output file.");
 22:     }
 23: 
 24:     public void connectToOutputFile( )
 25:     {
 26:         String outputFileName =
 27:                       getFileName("Enter output file name:");
 28:         try
 29:         {
 30:             outputStream = new ObjectOutputStream(
 31:                         new FileOutputStream(outputFileName));
 32:         }
 33:         catch(IOException e)
 34:         {
 35:             System.out.println("Error opening output file "
 36:                                              + outputFileName);
 37:             System.out.println(e.getMessage( ));
 38:             System.exit(0);
 39:         }
 40:     }
 41: 
 42:     public void connectToInputFile( )
 43:     {
 44:         String inputFileName =
 45:                      getFileName("Enter input file name:");
 46:         try
 47:         {
 48:             inputStream =
 49:               new ObjectInputStream(
 50:                              new FileInputStream(inputFileName));
 51:         }
 52:         catch(FileNotFoundException e)
 53:         {
 54:             System.out.println("File " + inputFileName
 55:                                        + " not found.");
 56:             System.exit(0);
 57:         }
 58:         catch(IOException e)
 59:         {
 60:             System.out.println("Error opening input file "
 61:                                              + inputFileName);
 62:             System.exit(0);
 63:         }
 64:     }
 65: 
 66:     private String getFileName(String prompt)
 67:     {
 68:         String fileName = null;
 69:         System.out.println(prompt);
 70:         Scanner keyboard = new Scanner(System.in);
 71:         fileName = keyboard.next( );
 72:         return fileName;
 73:     }
 74: 
 75:     public void timesTwo( )
 76:     {
 77:         int next;
 78:         try
 79:         {
 80:             while (true)
 81:             {
 82:                 next = inputStream.readInt( );
 83:                 outputStream.writeInt(2*next);
 84:             }
 85:         }
 86:         catch(EOFException e)
 87:         {
 88:             //Do nothing. This just ends the loop.
 89:         }
 90:         catch(IOException e)
 91:         {
 92:             System.out.println(
 93:                    "Error: reading or writing files.");
 94:             System.out.println(e.getMessage( ));
 95:             System.exit(0);
 96:         }
 97:     }
 98: 
 99:     public void closeFiles( )
100:     {
101:         try
102:         {
103:             inputStream.close( );
104:             outputStream.close( );
105:         }
106:         catch(IOException e)
107:         {
108:             System.out.println("Error closing files "
109:                                         + e.getMessage( ));
110:             System.exit(0);
111:         }
112:     }
113: }