📄 record.java
字号:
// Fig. 15.10: Record.java
// Record class for the RandomAccessFile programs.
import java.io.*;
public class Record {
private int account;
private String lastName;
private String firstName;
private double balance;
// Read a record from the specified RandomAccessFile
public void read( RandomAccessFile file ) throws IOException
{
account = file.readInt();
char first[] = new char[ 15 ];
for ( int i = 0; i < first.length; i++ )
first[ i ] = file.readChar();
firstName = new String( first );
char last[] = new char[ 15 ];
for ( int i = 0; i < last.length; i++ )
last[ i ] = file.readChar();
lastName = new String( last );
balance = file.readDouble();
}
// Write a record to the specified RandomAccessFile
public void write( RandomAccessFile file ) throws IOException
{
StringBuffer buf;
file.writeInt( account );
if ( firstName != null )
buf = new StringBuffer( firstName );
else
buf = new StringBuffer( 15 );
buf.setLength( 15 );
file.writeChars( buf.toString() );
if ( lastName != null )
buf = new StringBuffer( lastName );
else
buf = new StringBuffer( 15 );
buf.setLength( 15 );
file.writeChars( buf.toString() );
file.writeDouble( balance );
}
public void setAccount( int a ) { account = a; }
public int getAccount() { return account; }
public void setFirstName( String f ) { firstName = f; }
public String getFirstName() { return firstName; }
public void setLastName( String l ) { lastName = l; }
public String getLastName() { return lastName; }
public void setBalance( double b ) { balance = b; }
public double getBalance() { return balance; }
// NOTE: This method contains a hard coded value for the
// size of a record of information.
public static int size() { return 72; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -