处理事件时关闭缓冲的 Writer

Closing buffered Writer while handling event

我正在尝试使用我的 gui 中的按钮写信给我的 text file。问题是我无法在事件处理程序期间 close buffered writer 因为在第二次之后它会给我一个错误:

Stream closed

我打算在用户退出应用程序时关闭流。我该如何关闭它?

 package pkgfinal.project;

 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import javafx.application.Application;
 import javafx.beans.binding.BooleanBinding;
 import javafx.geometry.Insets;
 import javafx.scene.Scene;
 import javafx.scene.control.Button;
 import javafx.scene.control.Label;
 import javafx.scene.control.TextField;
 import javafx.scene.layout.GridPane;
 import javafx.stage.Stage;

 public class FinalProject extends Application {

private static File file;
private static FileWriter fw;
private static BufferedWriter output;

private final Button btnFrw = new Button("Next Record");
private final Button btnBck = new Button("Previous Record");
private final Button btnAdd = new Button("Add Record");
private final Button btnMod = new Button("Modify Record");
private final Button btnFrst = new Button("First Record");
private final Button btnLast = new Button("Last Record");

private final Label lblID = new Label("Customer ID");
private final Label lblG = new Label("Game");
private final Label lblBP = new Label("Buying price");
private final Label lblD = new Label("Date purchased");
private final Label lblCons = new Label("Console");
private final Label lblSP = new Label("Selling Price");

private TextField txtID = new TextField();
private TextField txtG = new TextField();
private TextField txtBP = new TextField();
private TextField txtD = new TextField();
private TextField txtCons = new TextField();
private TextField txtSP = new TextField();

@Override
public void start(Stage primaryStage) {
    try {
        file = new File("UsedGames.dat");
        fw = new FileWriter(file, true);
        output = new BufferedWriter(fw);
    } catch (Exception e) {
        System.out.println("Error creating file: " + e.getMessage());
    }
    GridPane gp = new GridPane();
    gp.setPadding(new Insets(30, 30, 30, 30));
    gp.setHgap(5);
    gp.setVgap(5);

    gp.add(lblID, 0, 0);
    gp.add(txtID, 0, 1);
    gp.add(lblG, 1, 0);
    gp.add(txtG, 1, 1);
    gp.add(lblBP, 2, 0);
    gp.add(txtBP, 2, 1);
    gp.add(lblD, 3, 0);
    gp.add(txtD, 3, 1);
    gp.add(lblSP, 4, 0);
    gp.add(txtSP, 4, 1);
    gp.add(lblCons, 5, 0);
    gp.add(txtCons, 5, 1);

    gp.add(btnAdd, 0, 3);
    gp.add(btnFrw, 1, 3);
    gp.add(btnBck, 2, 3);
    gp.add(btnMod, 3, 3);
    gp.add(btnFrst, 4, 3);
    gp.add(btnLast, 5, 3);

    BooleanBinding booleanBind;
    booleanBind = txtID.textProperty().isEmpty()
            .or(txtG.textProperty().isEmpty())
            .or(txtBP.textProperty().isEmpty()
                    .or(txtD.textProperty().isEmpty()
                            .or(txtSP.textProperty().isEmpty()
                                    .or(txtCons.textProperty().isEmpty()))));

    btnAdd.setOnMousePressed(e -> {
        try {   

            output.write(txtID.getText() + ", ");
            output.write(txtG.getText() + ", ");
            output.write(txtBP.getText() + ", ");
            output.write(txtD.getText() + ", ");
            output.write(txtCons.getText() + ", ");
            output.write(txtSP.getText());
            output.newLine();
            txtID.clear();
            txtG.clear();
            txtBP.clear();
            txtD.clear();
            txtCons.clear();
            txtSP.clear();
           output.close();//heres the problem

        } catch (Exception t) {
            System.out.println("An error has occured " + t.getMessage());

        }
    });



    btnAdd.disableProperty().bind(booleanBind);

    Scene scene = new Scene(gp, 1000, 175);
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    Application.launch(args);

}
}

output.close(); 移到 stage.setOnCloseRequest() 内。

Stage.setOnCloseRequest 在我们试图关闭舞台时被调用。 BufferedWriter 不会 关闭,除非 window 关闭并且您可以根据需要多次使用它。最后在退出应用前,关闭了。

...
primaryStage.setOnCloseRequest( event -> {
     output.close();
});
...