为银行创建随机访问文件

时间:2013-02-22 04:17:56

标签: java io random-access

我需要帮助以下我知道错误的部分:

  1. 空格/截断 - 我不知道如何做到这一点
  2. file.seek - 可能是错误的,因为我没有间距/截断为8个字符。
  3. 这是我的代码:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.RandomAccessFile;
    
    public class NationalBank
    {
      public static void main(String[] args)  
      {
      InputStreamReader temp = null;
      BufferedReader input = null;
      try
      {
         temp = new InputStreamReader(System.in);
         input = new BufferedReader(temp);
         int acct;
         double amount[] = new double[9999];
         String name[] = new String[9999];
         RandomAccessFile file = new RandomAccessFile("bank.txt", "rw");
         while(true)
         {
            System.out.println("Enter Account Number (0-9999): ");
            acct = Integer.parseInt(input.readLine());
            System.out.println("Enter Last Name: ");
            name[acct] = input.readLine();
            System.out.println("Enter Balance ");
            amount[acct] = Double.parseDouble(input.readLine());
            if(acct >=0 && acct <=9999) {
               file.seek(acct*10);
               file.writeBytes(" "+name[acct]);
               file.writeBytes(" "+amount[acct]);
            }
    
            System.out.println("Enter More? (y/n)");   
            if (input.readLine().toLowerCase().equals("n"))
               break;
         }
         file.close();
      }
         catch (Exception e)
         {  
         }
       }
    }
    

1 个答案:

答案 0 :(得分:1)

/**
 * Convert name from string into 8 bytes truncating and padding with spaces
 * id necessary.
 */
public static byte [] truncateName (String name)
{
    byte [] result = new byte [8];
    for (int i = 0; i < 8; i++)
        result [i] = i < name.length () ? (byte)name.charAt (i) : (byte)' ';
    return result;
}

/**
 * Convert double value into 8 bytes.
 */
public static byte [] packAmount (double amount)
{
    byte [] result = new byte [8];
    long bits = Double.doubleToLongBits (amount);

    for (int i = 0; i < 8; i++)
    {
        result [i] = (byte)(bits & 0xFF);
        bits >>>= 8;
    }

    return result;
}

public static void writeAccountinformation (
    RandomAccessFile file, int account, String name, double amount)
    throws IOException
{
    file.seek (account * 16); // 8 bytes for name and another 8 for amount
    file.write (truncateName (name));
    file.write (packAmount (amount));
}

public static void main(String[] args) throws Exception
{
    RandomAccessFile file = new RandomAccessFile ("bank.txt", "rw");
    try
    {
        BufferedReader reader = new BufferedReader (
            new InputStreamReader (System.in));

        while (true)
        {
            System.out.print ("Enter Account Number (0-9999): ");
            int account = Integer.parseInt (reader.readLine ());
            System.out.print ("Enter Last Name: ");
            String name = reader.readLine ();
            System.out.print ("Enter Balance: ");
            double amount = Double.parseDouble (reader.readLine ());

            writeAccountinformation (file, account, name, amount);

            System.out.println ("Enter More? (y/n)");
            if (reader.readLine ().toLowerCase ().equals ("n"))
                break;
        }
    }
    finally
    {
        file.close();
    }
}

稍后您可以从文件中读取数据,如下所示:

FileInputStream input = new FileInputStream ("bank.txt");
try
{
    byte [] record = new byte [16];
    while (input.read (record) == 16)
    {
        String name = new String (record, 0, 8);
        long bits = 0L;
        for (int i = 15; i >= 8; i--)
        {
            bits <<= 8;
            bits |= record [i] & 0xFF;
        }
        double amount = Double.longBitsToDouble (bits);

        System.out.println("Name: " + name + ", amount: " + amount);
    }
}
finally
{
    input.close ();
}