英文:
How to overwrite (save) a file when it has been edited?
问题
我正在开发一个小型数据管理应用程序,希望除了现有的“另存为 -> XML / CSV”菜单项之外,还能有另一个“保存”项。
此项应在不再打开对话框的情况下,覆盖上次加载的文件并保存更改后的数据(类似于 MS Excel 或 Word 中的操作)。
当然,我已经进行了一些研究,但没有找到合适的解决方案,或者可能搜索错了。
我已经拥有并且正在使用的这些条目和功能:
MenuItem saveXML = new MenuItem("另存为 XML");
saveXML.setOnAction((e) -> {
// 将文件保存为 XML
});
MenuItem saveCSV = new MenuItem("另存为 CSV");
saveCSV.setOnAction((e) -> {
// 将文件保存为 CSV
});
我想创建这个条目:
MenuItem save = new MenuItem("保存");
save.setOnAction((e) -> {
// 以相同格式(XML 或 CSV)保存上次打开的文档,无需任何对话框
});
英文:
I am currently developing a small data management application and would like to have another "Save" entry besides the existing "Save as -> XML / CSV" menu items.
This entry should overwrite the last loaded file with the changed data without opening a dialog again (like in MS Excel or Word).
Of course I have already done some research, but didn't find anything suitable or searched for the wrong one.
These entries and functions I already have and they work:
MenuItem saveXML = new MenuItem("Save as XML");
saveXML.setOnAction((e) -> {
// save file as XML
});
MenuItem saveCSV = new MenuItem("Save as CSV");
saveCSV.setOnAction((e) -> {
// save file as CSV
});
I would like to create this entry:
MenuItem save = new MenuItem("Save");
saveCSV.setOnAction((e) -> {
// Save the last opened document in the same format (XML or CSV) without any dialog
});
答案1
得分: 0
根据Abra的建议,您必须通过将其存储在私有字段中来记住最近加载文件的位置:
private File currentFile;
然后在加载文件时进行设置:
MenuItem load = new MenuItem("Load");
load.setOnAction(e -> {
FileChooser chooser = new FileChooser();
// (在此设置FileChooser的标题和过滤器)
File file = chooser.showOpenDialog(stage);
if (file != null) {
// (将文件加载到应用程序中)
this.currentFile = file;
}
});
现在,您可以使用currentFile
的值进行保存:
MenuItem save = new MenuItem("Save");
save.setOnAction(e -> {
if (currentFile != null) {
// (写入currentFile)
} else {
// 新文件;询问用户保存位置。
FileChooser chooser = new FileChooser();
// (在此设置FileChooser的标题和过滤器)
File file = chooser.showSaveDialog(stage);
if (file != null) {
// (根据选择的过滤器将文件保存为CSV或XML)
this.currentFile = file;
}
}
});
如果尚未完成,您可能希望将保存逻辑移到私有方法中。
英文:
As Abra suggested, you must remember the location of the most recently loaded file by storing it in a private field:
private File currentFile;
Then you set it whenever you load a file:
MenuItem load = new MenuItem("Load");
load.setOnAction(e -> {
FileChooser chooser = new FileChooser();
// (set FileChooser's title and filters here)
File file = chooser.showOpenDialog(stage);
if (file != null) {
// (load file into application)
this.currentFile = file;
}
});
Now, you can use that currentFile
value to save:
MenuItem save = new MenuItem("Save");
save.setOnAction(e -> {
if (currentFile != null) {
// (write to currentFile)
} else {
// New file; ask user for save location.
FileChooser chooser = new FileChooser();
// (set FileChooser's title and filters here)
File file = chooser.showSaveDialog(stage);
if (file != null) {
// (save file as CSV or XML depending on chosen filter)
this.currentFile = file;
}
}
});
You may want to move your save logic into private methods, if you haven’t already.
答案2
得分: 0
我有一个对我有效的解决方案。这可能不是最优解,但仍然可能有用。
我将一个包含文件路径的属性传递给我的menuItem,例如 .put("FILE_LOCATION",..),然后在保存事件中使用这个属性来选择最后一个路径。
由于我还想自动保存为CSV或XML文件,我会从包含另一个路径的变量中删除该路径,如果存在的话。例如 .remove("FILE_LOCATION")
loadCSV.setOnAction(e -> {
...
File f = fileChooser.showOpenDialog(null);
loadCSV.getProperties().put("FILE_LOCATION", f.getAbsolutePath());
loadXML.getProperties().remove("FILE_LOCATION");
});
然后我检查哪个变量已填充,然后运行相应的方法。
save.setOnAction(e -> {
try {
if (loadCSV.getProperties().isEmpty() == false) {
File saveCSV = new File(loadCSV.getProperties().get("FILE_LOCATION").toString());
if (saveCSV != null) {
CSV.writeCSV(saveCSV, DataHandler.INSTANCE.foodlist());
}
} else if (loadXML.getProperties().isEmpty() == false) {
File saveXML = new File(loadXML.getProperties().get("FILE_LOCATION").toString());
if (saveXML != null) {
XML.writeXMLList(saveXML, DataHandler.INSTANCE.foodlist());
}
}
});
英文:
I have a solution that works for me. It may not be the optimal solution, but it could still be useful.
I pass a property to my menuItem that contains the path of my file .put("FILE_LOCATION",..) and use this property in the save-event to select the last path.
Since I also want to save automatically as either CSV or XML file, I delete the path from the variable that contains the other one, if it exists. .remove("FILE_LOCATION")
loadCSV.setOnAction(e -> {
...
File f = fileChooser.showOpenDialog(null);
loadCSV.getProperties().put("FILE_LOCATION", f.getAbsolutePath());
loadXML.getProperties().remove("FILE_LOCATION");
});
Then I check which variable is filled and then run the respective method.
save.setOnAction(e -> {
try {
if (loadCSV.getProperties().isEmpty() == false) {
File saveCSV = new File(loadCSV.getProperties().get("FILE_LOCATION").toString());
if (saveCSV != null) {
CSV.writeCSV(saveCSV, DataHandler.INSTANCE.foodlist());
}
} else if (loadXML.getProperties().isEmpty() == false) {
File saveXML = new File(loadXML.getProperties().get("FILE_LOCATION").toString());
if (saveXML != null) {
XML.writeXMLList(saveXML, DataHandler.INSTANCE.foodlist());
}
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论