public class CreateRandomFile
1: // Fig. 14.24: CreateRandomFile.java
2: // Creates random-access file by writing 100 empty records to disk.
3: import java.io.IOException;
4: import java.io.RandomAccessFile;
5:
6: import com.deitel.jhtp6.ch14.RandomAccessAccountRecord;
7:
8: public class CreateRandomFile
9: {
10: private static final int NUMBER_RECORDS = 100;
11:
12: // enable user to select file to open
13: public void createFile()
14: {
15: RandomAccessFile file = null;
16:
17: try // open file for reading and writing
18: {
19: file = new RandomAccessFile( "clients.dat", "rw" );
20:
21: RandomAccessAccountRecord blankRecord =
22: new RandomAccessAccountRecord();
23:
24: // write 100 blank records
25: for ( int count = 0; count < NUMBER_RECORDS; count++ )
26: blankRecord.write( file );
27:
28: // display message that file was created
29: System.out.println( "Created file clients.dat." );
30:
31: System.exit( 0 ); // terminate program
32: } // end try
33: catch ( IOException ioException )
34: {
35: System.err.println( "Error processing file." );
36: System.exit( 1 );
37: } // end catch
38: finally
39: {
40: try
41: {
42: if ( file != null )
43: file.close(); // close file
44: } // end try
45: catch ( IOException ioException )
46: {
47: System.err.println( "Error closing file." );
48: System.exit( 1 );
49: } // end catch
50: } // end finally
51: } // end method createFile
52: } // end class CreateRandomFile
53:
54: /*************************************************************************
55: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
56: * Pearson Education, Inc. All Rights Reserved. *
57: * *
58: * DISCLAIMER: The authors and publisher of this book have used their *
59: * best efforts in preparing the book. These efforts include the *
60: * development, research, and testing of the theories and programs *
61: * to determine their effectiveness. The authors and publisher make *
62: * no warranty of any kind, expressed or implied, with regard to these *
63: * programs or to the documentation contained in these books. The authors *
64: * and publisher shall not be liable in any event for incidental or *
65: * consequential damages in connection with, or arising out of, the *
66: * furnishing, performance, or use of these programs. *
67: *************************************************************************/