不使用javafx将.txt文件保存在文件夹中

时间:2017-09-19 18:51:44

标签: javafx netbeans

我创建了一个小应用程序但没有txt文件没有保存在文件夹中..它通过文件夹的名称保存在外部文件夹...代码如下。

public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;
    @FXML
    private Label label1;
    @FXML
    private Button button;
    @FXML
    private TextField txt;
    @FXML
    private Button save;

    Stage primaryStage;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        // System.out.println("You clicked me!");
        //label.setText("Hello World!");
        DirectoryChooser dc= new DirectoryChooser();
        File chosenDir = dc.showDialog(primaryStage);
        System.out.println(""+chosenDir);
    }

    @FXML
    private void save(ActionEvent event) throws IOException {
        DirectoryChooser dc= new DirectoryChooser(); 
        File chosenDir = dc.showDialog(primaryStage);
        StringBuilder sb= new StringBuilder();
        sb.append(txt.getText().toString());
        File f= new File(chosenDir+"user.txt");
        FileWriter f1=new FileWriter(f);
        f1.write(sb.toString());
        f1.close();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

0 个答案:

没有答案