首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用FileeChooser在javaFX中创建新文件并将数据保存在文件中?

如何使用FileeChooser在javaFX中创建新文件并将数据保存在文件中?
EN

Stack Overflow用户
提问于 2020-05-26 12:57:18
回答 3查看 2.5K关注 0票数 0

我需要通过对话框保存文件。对话框打开,但文件本身未保存,如何修复?代码:

代码语言:javascript
运行
复制
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Save");
    fileChooser.setInitialFileName("save file");
    fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("All Files", "*.*"));
    //Adding action on the menu item
    save.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {


            //Opening a dialog box
            fileChooser.getExtensionFilters()
                    .addAll(
                            new FileChooser.ExtensionFilter("TXT files (*.TXT)", "*.TXT"),
                            new FileChooser.ExtensionFilter("txt files (*.txt)", "*.txt")
                    );
            fileChooser.showSaveDialog(primaryStage);

        }
    });
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-05-26 15:18:15

下面是一个小示例,如果您希望创建一个具有特定文件名(TextField)、文件扩展名(ComboBox)和目标目录(DirectoryChooser)的新文件:

控制员类别:

代码语言:javascript
运行
复制
package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    private Button
            saveBtn;
    @FXML
    private TextField
            fileNameTextField,
            directoryTextField;
    @FXML
    private ComboBox<String>
            fileExtensionComboBox;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        fileExtensionComboBox.getItems().addAll(".txt", ".json", ".xml");

        saveBtn.disableProperty().bind(fileNameTextField.textProperty().isEmpty()
                .or(fileExtensionComboBox.getSelectionModel().selectedItemProperty().isNull()
                        .or(directoryTextField.textProperty().isEmpty())));
    }

    @FXML
    public void handleChangeDirectoryBtnClick() {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        File selectedDirectory = directoryChooser.showDialog(null);
        if (selectedDirectory != null) directoryTextField.setText(selectedDirectory.getAbsolutePath());
    }

    @FXML
    public void handleSaveBtnClick() {
        Path file = Paths.get(directoryTextField.getText(), fileNameTextField.getText().trim() + fileExtensionComboBox.getSelectionModel().getSelectedItem());
        if(!Files.exists(file)) { {
            try {
                Files.createFile(file.toAbsolutePath());
                //...
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

FXML文件:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane hgap="3.0" vgap="3.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" />
      <ColumnConstraints hgrow="SOMETIMES" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints vgrow="SOMETIMES" />
      <RowConstraints vgrow="SOMETIMES" />
      <RowConstraints vgrow="SOMETIMES" />
      <RowConstraints vgrow="SOMETIMES" />
      <RowConstraints vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <Label text="File Name:" />
      <TextField fx:id="fileNameTextField" GridPane.rowIndex="1" />
      <Label text="File Extension:" GridPane.columnIndex="1" />
      <ComboBox fx:id="fileExtensionComboBox" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
      <Label text="Directory:" GridPane.rowIndex="2" />
      <AnchorPane GridPane.columnSpan="2" GridPane.rowIndex="3">
         <children>
            <TextField fx:id="directoryTextField" editable="false" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="60.0" />
            <Button onAction="#handleChangeDirectoryBtnClick" text="Change" AnchorPane.rightAnchor="0.0" />
         </children>
      </AnchorPane>
      <HBox alignment="CENTER" GridPane.columnSpan="2" GridPane.rowIndex="4">
         <children>
            <Button fx:id="saveBtn" onAction="#handleSaveBtnClick" text="Save" />
         </children>
      </HBox>
   </children>
   <padding>
      <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
   </padding>
</GridPane>

预览:

票数 0
EN

Stack Overflow用户

发布于 2020-05-26 16:35:57

FileChooser不会在存储设备上创建文件,如果文件存在,它也不会修改该文件。

这只是让用户通过保存对话框确定的File实例的一种方法。在用户选择文件之后,您需要自己保存数据:

代码语言:javascript
运行
复制
File selectedFile = fileChooser.showSaveDialog(primaryStage);
if (selectedFile != null) {
    // dialog closed by selecting a file to save the data to

    // write data here yourself, e.g.
    try (BufferedReader br = Files.newBufferedReader(selectedFile.toPath(), StandardCharsets.UTF_8)) {
        br.write("Hello World!\n");
    }
}

如果写入文件可能需要很长时间,则应该将写入逻辑移动到后台线程,以避免冻结GUI。

票数 1
EN

Stack Overflow用户

发布于 2020-05-26 13:03:08

FileChooser中的筛选器仅用于显示现有文件。如果要保存具有特定扩展名的文件,则需要如下所示:

代码语言:javascript
运行
复制
String fileName = file.toString();
if (!fileName.endsWith(".java"))
fileName += ".java";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62022692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档