2013-02-22 22 views
0

創建隨機訪問文件,我需要用下面的部分,我知道錯了幫助:對銀行

  1. 的空間/截斷 - 我不知道如何做到這一點
  2. 的file.seek - 可能錯誤,因爲我不是間距/截斷到8個字符。

這裏是我的代碼:

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) 
    { 
    } 
    } 
} 
+0

啊,數據庫?有很多獨立的單用戶數據庫[HyperSQL](http://hsqldb.org/)和[H2](http://www.h2database.com/html/main.html)。 – MadProgrammer 2013-02-22 04:20:05

+0

@MadProgrammer - 我敢肯定這是一種練習。 – Perception 2013-02-22 04:22:06

+0

您乘以10,名稱使用8個字節,那麼如何將數量填入2個字節? – 2013-02-22 04:24:48

回答

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(); 
} 
+0

所以我甚至不需要一個數組? – Acitropy 2013-02-22 04:50:52

+0

我該怎麼辦呢如果我不得不另一個應用程序顯示所有的帳戶有效的姓氏和餘額? – Acitropy 2013-02-22 05:02:43

+0

@ user1913362添加了讀取數據的代碼片段。 – 2013-02-22 05:49:24