3

该程序应该在 .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; }
}
4

3 回答 3

1

除了其他人指出的任何其他问题。

   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() );
}
于 2013-08-06T00:53:04.110 回答
0

我绝对不是专家,但我确实在您的代码中看到了一些错误。在这一行中,您将文件指针设置为指向所需的记录:

// Go to the record location
file.seek( (recordNumber - 1) * acc.size() );

但是,在您的帐户类中,您将字段大小设置为 20:

public static int size() { return 20; }

仔细查看 java API 会发现您在此处使用的 writeChars 方法 -

 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() );

}

被写成一系列字符,这是重要的部分,每个都写成一个两字节的值。现在,当您将指针设置为特定记录时,搜索值以字节而不是字符为单位。因此,当您打开文件并将指针设置为大于第一条记录的特定记录时,您将覆盖数据,因为您的字段的实际大小是 40,而不是 20(记住字节,而不是字符)。希望我有所帮助。

于 2013-09-08T23:15:53.187 回答
0

问题是这样的:

// Create 100000 blank records in the file
for ( int i = 0; i < 100001; i++)
    acc.write( file );

你在你的initialize()函数中有那个,它被调用CreateBankFile(),被调用main()

所以每次运行程序时,文件都会重新初始化。歼灭。因此,删除该代码,您的程序应该可以工作。

(虽然您必须第一次初始化文件,但可能会制作第二个程序来初始化文件或将其作为用户选项,或者检测文件是否存在并在它不存在时对其进行初始化。)

于 2013-08-06T00:54:37.073 回答