Java RandomAccessFile,仅查找第一个条目

时间:2014-04-21 14:05:58

标签: java

当我的程序从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();
}

}

1 个答案:

答案 0 :(得分:0)

我猜是

data.getAccount()!= 0

所以你的循环只执行一次,因为你是在do ... while();

中完成的

尝试在代码中添加一些调试,并确保data.getAccount()等于。