程序应该在一个.txt文件中创建一些空白记录,这些记录可以在以后编辑以添加诸如名称和余额等信息。我的问题是,每当我放入另一个记录时,第一个帐户的余额就会消失(但名称不会消失)。如果我尝试添加另一个帐户,以前帐户的余额也会消失。可能被重写了什么的。
我该怎么解决?
import java.io.*;
public class CreateBankFile {
private RandomAccessFile file;
public static void main(String[] args) throws IOException {
CreateBankFile blank = new CreateBankFile();
}
public CreateBankFile() throws IOException{
initialize();
int task = 0;
while(task == 0)
{
writeRecord();
}
}
private void initialize() {
Account acc = new Account();
try {
File fileName = new File("C:/Documents and Settings/x/Desktop/hehe.txt");
file = new RandomAccessFile( fileName, "rw" );
// Create 100000 blank records in the file
for ( int i = 0; i < 100001; i++)
acc.write( file );
} catch ( IOException ioex ){
System.out.println("File does not exists");
}
}
private void writeRecord() throws IOException {
Account acc = new Account();
BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Account # ");
String accNo = data.readLine();
int recordNumber = Integer.parseInt(accNo);
try {
File fileName = new File("C:/Documents and Settings/x/Desktop/hehe.txt");
file = new RandomAccessFile( fileName, "rw" );
// Write the record to the file
if ( recordNumber > 0 && recordNumber < 100001 ) {
System.out.println("Enter Account Name: ");
String neym = data.readLine();
System.out.println("Enter Remaining Balance: ");
String numbah = data.readLine();
acc.setName(neym);
acc.setBalance(numbah);
// Go to the record location
file.seek( (recordNumber - 1) * acc.size() );
// Write the record out
acc.write( file );
System.out.println("\nUpdate Succesful!\n");
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
} catch ( Exception ioex ){
System.out.println("File does not exists");
}
}
}
class Account {
private String name;
private String balance;
// Constructor
public Account() { this ("", ""); }
// Overloaded Constructor
public Account(String n, String b) {
name = n;
balance = b;
}
// Public method to read the data from the file
public void read( RandomAccessFile file ) throws IOException {
setName( padData( file ) );
setBalance ( padData( file ) );
}
// Public method to write the data to the file
public void write( RandomAccessFile file ) throws IOException {
writeData( file, getName() );
writeData( file, getBalance() );
}
// The method that actually writes the data to the file
private void writeData(RandomAccessFile f, String n) throws IOException {
StringBuffer buf = null;
if ( n != null )
buf = new StringBuffer( n );
else
buf = new StringBuffer( 20 );
buf.setLength( 20 );
f.writeChars( buf.toString() );
}
// This pads the data so to make the data all the same size
// we will go for a size of 20
private String padData( RandomAccessFile f ) throws IOException {
char data[] = new char[ 20 ], temp;
for ( int i = 0; i < data.length; i++ ) {
temp = f.readChar();
data[ i ] = temp;
}
return new String ( data ).replace( '\0', ' ' );
}
// This method hard codes the value for the size of the record
// which is 20
public static int size() { return 20; }
// The get and set methods
public void setName(String n) { name = n; }
public void setBalance(String b) { balance = b; }
public String getName() { return name; }
public String getBalance() { return balance; }
}
最佳答案
除了其他人指出的问题。
buf.setLength( 20 );
你只写了20个字。您还需要在if语句上使用大括号:
private void writeData(RandomAccessFile f, String n) throws IOException {
StringBuffer buf = null;
if ( n != null )
buf = new StringBuffer( n );
else {
buf = new StringBuffer( 20 );
buf.setLength( 20 );
}
f.writeChars( buf.toString() );
}