26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

930 Files and Streams Chapter 16<br />

33 // ensure that name is proper length<br />

34 private String padName( RandomAccessFile file )<br />

35 throws IOException<br />

36 {<br />

37 char name[] = new char[ 15 ], temp;<br />

38<br />

39 for ( int count = 0; count < name.length; count++ ) {<br />

40 temp = file.readChar();<br />

41 name[ count ] = temp;<br />

42 }<br />

43<br />

44 return new String( name ).replace( '\0', ' ' );<br />

45 }<br />

46<br />

47 // write a record <strong>to</strong> specified RandomAccessFile<br />

48 public void write( RandomAccessFile file ) throws IOException<br />

49 {<br />

50 file.writeInt( getAccount() );<br />

51 writeName( file, getFirstName() );<br />

52 writeName( file, getLastName() );<br />

53 file.writeDouble( getBalance() );<br />

54 }<br />

55<br />

56 // write a name <strong>to</strong> file; maximum of 15 characters<br />

57 private void writeName( RandomAccessFile file, String name )<br />

58 throws IOException<br />

59 {<br />

60 StringBuffer buffer = null;<br />

61<br />

62 if ( name != null )<br />

63 buffer = new StringBuffer( name );<br />

64 else<br />

65 buffer = new StringBuffer( 15 );<br />

66<br />

67 buffer.setLength( 15 );<br />

68 file.writeChars( buffer.<strong>to</strong>String() );<br />

69 }<br />

70<br />

71 // NOTE: This method contains a hard coded value for the<br />

72 // size of a record of information.<br />

73 public static int size()<br />

74 {<br />

75 return 72;<br />

76 }<br />

77<br />

78 } // end class RandomAccessAccountRecord<br />

Fig. Fig. Fig. 16.11 16.11 16.11 RandomAccessAccountRecord class used in the random-access file<br />

programs (part 2 of 2).<br />

Class RandomAccessAccountRecord inherits AccountRecord’s (Fig. 16.5)<br />

implementation, which includes private instance variables—account, lastName,<br />

firstName and balance—as well as their public set and get methods.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!