处理事件时关闭缓冲的Writer

时间:2015-04-15 20:01:58

标签: javafx mouseevent

我正在尝试使用我的gui中的按钮写入text file。问题是我无法在事件处理程序中关闭 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);

}
}

1 个答案:

答案 0 :(得分:1)

stage.setOnCloseRequest()内移动output.close();

当我们试图关闭舞台时,会调用

Stage.setOnCloseRequest。除非窗口关闭,否则BufferedWriter 将不会关闭,您可以根据需要多次使用它。最后,在退出申请之前,它已经关闭。

...
primaryStage.setOnCloseRequest( event -> {
     output.close();
});
...    
相关问题