簡體   English   中英

Java RandomAccessFile ,只找到第一個條目

[英]Java RandomAccessFile , Only finds the first entry

當我的程序從 randomaccessfile 中讀取時,它只會找到第一個文件或具有最低帳號的文件(這是一個銀行程序)之后,我收到了帶有讀取錯誤的 IO 異常

private RandomAccessFile input; // Random Aecess File input Stream

private Record data;

public static JFrame frame = new JFrame();


public CredRead() // Constructor CredRead created


{

    // open the file
    try {

        // declare the output stream object and associate it to file
        // file.dat
        input = new RandomAccessFile("UnionDB.dat", "rw");

    }

    catch (IOException e) {
        // if an error occurs display a message on the screen
        System.err.println("File not opened properly\n " + e.toString());

        // the program terminates due to error
        System.exit(1);
    }

    data = new Record();

    setPreferredSize(new Dimension(650, 400));
    frame.setSize(getPreferredSize()); // Frame Size
    frame.setLocationRelativeTo(null);
    frame.setLayout(new GridLayout(7, 2)); // Grid Layout set

    /* GUI Components */

    frame.add(new Label("Enter Account Number and click Enter"));
    account_num = new TextField();
    frame.add(account_num);
    account_num.addActionListener(this);

    frame.add(new Label("First Name"));
    first_name = new TextField(20);
    first_name.setEditable(false);
    frame.add(first_name);

    frame.add(new Label("Last Name"));
    last_name = new TextField(20);
    last_name.setEditable(false);
    frame.add(last_name);

    frame.add(new Label("Available Funds"));
    balance = new TextField(20);
    balance.setEditable(false);
    frame.add(balance);

    frame.add(new Label("Overdraft Limit"));
    overdraft = new TextField(20);
    overdraft.setEditable(false);
    frame.add(overdraft);

    enter = new Button("Enter");
    enter.addActionListener(this);
    frame.add(enter);

    done = new Button("Click to Exit");
    done.addActionListener(this);
    frame.add(done);

    setVisible(true); // GUI components set as visible to the program

}

public void readRecord() {
    DecimalFormat twoDigits = new DecimalFormat("0.00");

    try {

        do {
            data.read(input);
        } while (data.getAccount() == 0);



        input.seek(
                  (long) ( data.getAccount()-1 ) * Record.size());
                        data.write( input );

        account_num.setText(String.valueOf(data.getAccount()));
        first_name.setText(data.getFirstName());
        last_name.setText(data.getLastName());
        balance.setText(String.valueOf(twoDigits.format(data.getBalance())));
        overdraft.setText(String.valueOf(twoDigits.format(data.getOverdraft())));

    }// end try

    catch (EOFException eof) {
        closeFile();
    }

    catch (IOException e) {
        // if an error occurs display a message on the screen
        System.err.println("Error during read from file\n " + e.toString());
        // the program terminates due to error
        // System.exit(1);
    }
}

private void closeFile() {
    try {
        input.close();
        // System.exit(1);
    } catch (IOException e) {
        // if an error occurs display a message on the screen
        System.err.println("Error closing file\n " + e.toString());
        // System.exit(1);
    }
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == enter)
        readRecord();
    else if (e.getSource() == done)
        frame.dispose();
    else
        frame.add(new TextField(" Account Not Found on Database "));
    closeFile();

}

public static void main(String args[]) {
    new CredRead();
}

}

我的猜測是

數據.getAccount() != 0

所以你的循環只執行一次,因為你是在 do ... while();

嘗試在您的代碼中添加一些調試並確保 data.getAccount() 等於什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM