2013-08-06 106 views
3

該程序應該創建一個.txt文件的空白記錄的數量,它可以稍後編輯,以添加名稱和平衡..等信息。我的問題是每當我放置另一條記錄時,第一個帳戶的餘額就會消失(但名稱不會)。如果我嘗試添加另一個帳戶,則以前帳戶的餘額也會消失。它可能被覆蓋或什麼的。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; } 
} 
+0

您是否嘗試過使用調試器逐步執行文件操作? –

+0

在你對這個解決方案投入太多之前,讓我指出,你的解決方案依賴於你的文本文件中所有'記錄'的長度完全相同。一旦用戶在記事本或其他文本編輯器中打開該文件,所有投注都將關閉。實際上,您編寫程序的方式只會存在一條「線」,因爲每條固定長度記錄/行末尾都沒有新行或crlf對。在我看來,你正在努力實現的可以使用電子表格來完成,而不需要任何代碼。 –

回答

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

問題是這樣的:

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

你必須在你的initialize()功能,這就是所謂的CreateBankFile()被稱爲在main()

所以每次運行程序時,都會重新初始化文件。殲滅。所以,刪除那些代碼,你的程序應該可以工作。

(儘管您必須首次初始化文件,也許需要第二個程序來初始化文件或者是用戶選項,或者檢測文件是否存在,並在文件尚未存在時進行初始化有)

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(請記住字節,而不是字符),因此您將覆蓋數據。希望我幫助。