hi: how can get various size to a random access file progarm .
i mean i have read that we should give a costant size tu constructor ,is it ture?
i mean i have read that we should give a costant size tu constructor ,is it ture?
4
5 import java.io.RandomAccessFile;
6 import java.io.IOException;
7
8 public class RandomAccessAccountRecord extends AccountRecord
9 {
10 public static final int SIZE = 72;//here a size of records is spesified
11
12 // no-argument constructor calls other constructor with default values
13 public RandomAccessAccountRecord()
14 {
15 this ( 0, "", "", 0.0 );
16 } // end no-argument RandomAccessAccountRecord constructor
17
18 // initialize a RandomAccessAccountRecord
19 public RandomAccessAccountRecord( int account, String firstName,
20 String lastName, double balance )
21 {
22 super( account, firstName, lastName, balance );
23 } // end four-argument RandomAccessAccountRecord constructor
24
25 // read a record from specified RandomAccessFile
26 public void read( RandomAccessFile file ) throws IOException
27 {
28 setAccount( file.readInt() );
29 setFirstName( readName( file ) );
30 setLastName( readName( file ) );
31 setBalance( file.readDouble() );
32 } // end method read
33
34 // ensure that name is proper length
35 private String readName( RandomAccessFile file ) throws IOException
36 {
37 char name[] = new char[ 15 ], temp;
38
39 for ( int count = 0; count < name.length; count++ )
40 {
41 temp = file.readChar();
42 name[ count ] = temp;
43 } // end for
44
45 return new String( name ).replace( '\0', ' ' );
46 } // end method readName
47
48 // write a record to specified RandomAccessFile
49 public void write( RandomAccessFile file ) throws IOException
50 {
51 file.writeInt( getAccount() );
52 writeName( file, getFirstName() );
53 writeName( file, getLastName() );
54 file.writeDouble( getBalance() );
55 } // end method write
56
57 // write a name to file; maximum of 15 characters
58 private void writeName( RandomAccessFile file, String name )
59 throws IOException
60 {
61 StringBuffer buffer = null;
62
63 if ( name != null )
64 buffer = new StringBuffer( name );
65 else
66 buffer = new StringBuffer( 15 );
67
68 buffer.setLength( 15 );
69 file.writeChars( buffer.toString() );
70 } // end method writeName
71 } // end class RandomAccessAccountRecord
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
public static final SIZE 72;
Comment