简体   繁体   English

JavaFX我的文件保存不起作用

[英]JavaFX My File Save isn't working

I have this shopping basket see here https://i.stack.imgur.com/9ty3E.png 我有这个购物篮,请看这里https://i.stack.imgur.com/9ty3E.png

And everything works but my File save, what's meant to happen is when you file save and an existing project is open and it overwrites the old xml files but it's completely skipping over it and going to the else statement. 一切正常,但我的文件保存有效,这意味着当您保存文件并打开一个现有项目时,它会覆盖旧的xml文件,但完全跳过了它,转到了else语句。

@FXML
private void handleSave() {
    File itemFile = mainApp.getItemFilePath();
    if (itemFile != null) {
        mainApp.saveItemDataToFile(itemFile);
    } else {
        handleSaveAs();
    }
}

And heres my Save as code for reference 这是我的另存为代码以供参考

@FXML
private void handleSaveAs() {
    FileChooser fileChooser = new FileChooser();

    // Set extension filter
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
            "XML files (*.xml)", "*.xml");
    fileChooser.getExtensionFilters().add(extFilter);

    // Show save file dialog
    File file = fileChooser.showSaveDialog(mainApp.getPrimaryStage());

    if (file != null) {
        // Make sure it has the correct extension
        if (!file.getPath().endsWith(".xml")) {
            file = new File(file.getPath() + ".xml");
        }
        mainApp.saveItemDataToFile(file);
    }
}

public File getItemFilePath() {
        Preferences prefs = Preferences.userNodeForPackage(MainApp.class);
        String filePath = prefs.get("Filepath", null);
        if (filePath != null) {
            return new File(filePath);
        } else {
            return null;
        }
    }
public void setItemFilePath(File file) {
    Preferences prefs = Preferences.userNodeForPackage(MainApp.class);
    if (file != null) {
        prefs.put("filePath", file.getPath());

        primaryStage.setTitle("Shopping Basket - " + file.getName());
    } else {
        prefs.remove("filePath");
        primaryStage.setTitle("Shopping Basket");
    }
}


public void loadItemDataFromFile(File file) {
        try {
            JAXBContext context = JAXBContext
                    .newInstance(BasketListWrapper.class);
            Unmarshaller um = context.createUnmarshaller();

            BasketListWrapper wrapper = (BasketListWrapper) um.unmarshal(file);

            itemData.clear();
            itemData.addAll(wrapper.getItems());


            setItemFilePath(file);

        } catch (Exception e) {
            Alert alert = new Alert(AlertType.ERROR);
            alert.setTitle("Error");
            alert.setHeaderText("Could not load data");
            alert.setContentText("Could not load data from file:\n"
                    + file.getPath());

            alert.showAndWait();

        }
    }

    public void saveItemDataToFile(File file) {
        try {
            JAXBContext context = JAXBContext
                    .newInstance(BasketListWrapper.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            BasketListWrapper wrapper = new BasketListWrapper();
            wrapper.setItems(itemData);

            m.marshal(wrapper, file);

            setItemFilePath(file);
        } catch (Exception e) {
            Alert alert = new Alert(AlertType.ERROR);
            alert.setTitle("Error");
            alert.setHeaderText("Could not load data");
            alert.setContentText("Could not load data from file:\n"
                    + file.getPath());

            alert.showAndWait();
        }
    }

In getItemFilePath method you're trying to get the " F ilepath" preference: getItemFilePath方法中,您尝试获取“ F ilepath”首选项:

String filePath = prefs.get("Filepath", null);

But in the setItemFilePath method you're setting the "file P ath" preference: 但是在setItemFilePath方法中,您要设置“ file P ath”首选项:

prefs.put("filePath", file.getPath());

Since preferences keys are case sensitive, just like any other String , you're always getting the default value, which is null. 由于首选项键区分大小写,就像其他String ,您始终会得到默认值null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM