如何在文件被编辑后覆盖(保存)该文件?

huangapple go评论129阅读模式
英文:

How to overwrite (save) a file when it has been edited?

问题

我正在开发一个小型数据管理应用程序,希望除了现有的“另存为 -> XML / CSV”菜单项之外,还能有另一个“保存”项。

此项应在不再打开对话框的情况下,覆盖上次加载的文件并保存更改后的数据(类似于 MS Excel 或 Word 中的操作)。

当然,我已经进行了一些研究,但没有找到合适的解决方案,或者可能搜索错了。

我已经拥有并且正在使用的这些条目和功能:

  1. MenuItem saveXML = new MenuItem("另存为 XML");
  2. saveXML.setOnAction((e) -> {
  3. // 将文件保存为 XML
  4. });
  5. MenuItem saveCSV = new MenuItem("另存为 CSV");
  6. saveCSV.setOnAction((e) -> {
  7. // 将文件保存为 CSV
  8. });

我想创建这个条目:

  1. MenuItem save = new MenuItem("保存");
  2. save.setOnAction((e) -> {
  3. // 以相同格式(XML 或 CSV)保存上次打开的文档,无需任何对话框
  4. });

这是我的应用程序

英文:

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:

  1. MenuItem saveXML = new MenuItem("Save as XML");
  2. saveXML.setOnAction((e) -> {
  3. // save file as XML
  4. });
  5. MenuItem saveCSV = new MenuItem("Save as CSV");
  6. saveCSV.setOnAction((e) -> {
  7. // save file as CSV
  8. });

I would like to create this entry:

  1. MenuItem save = new MenuItem("Save");
  2. saveCSV.setOnAction((e) -> {
  3. // Save the last opened document in the same format (XML or CSV) without any dialog
  4. });

This is my application

答案1

得分: 0

根据Abra的建议,您必须通过将其存储在私有字段中来记住最近加载文件的位置:

  1. private File currentFile;

然后在加载文件时进行设置:

  1. MenuItem load = new MenuItem("Load");
  2. load.setOnAction(e -> {
  3. FileChooser chooser = new FileChooser();
  4. // (在此设置FileChooser的标题和过滤器)
  5. File file = chooser.showOpenDialog(stage);
  6. if (file != null) {
  7. // (将文件加载到应用程序中)
  8. this.currentFile = file;
  9. }
  10. });

现在,您可以使用currentFile的值进行保存:

  1. MenuItem save = new MenuItem("Save");
  2. save.setOnAction(e -> {
  3. if (currentFile != null) {
  4. // (写入currentFile)
  5. } else {
  6. // 新文件;询问用户保存位置。
  7. FileChooser chooser = new FileChooser();
  8. // (在此设置FileChooser的标题和过滤器)
  9. File file = chooser.showSaveDialog(stage);
  10. if (file != null) {
  11. // (根据选择的过滤器将文件保存为CSV或XML)
  12. this.currentFile = file;
  13. }
  14. }
  15. });

如果尚未完成,您可能希望将保存逻辑移到私有方法中。

英文:

As Abra suggested, you must remember the location of the most recently loaded file by storing it in a private field:

  1. private File currentFile;

Then you set it whenever you load a file:

  1. MenuItem load = new MenuItem("Load");
  2. load.setOnAction(e -> {
  3. FileChooser chooser = new FileChooser();
  4. // (set FileChooser's title and filters here)
  5. File file = chooser.showOpenDialog(stage);
  6. if (file != null) {
  7. // (load file into application)
  8. this.currentFile = file;
  9. }
  10. });

Now, you can use that currentFile value to save:

  1. MenuItem save = new MenuItem("Save");
  2. save.setOnAction(e -> {
  3. if (currentFile != null) {
  4. // (write to currentFile)
  5. } else {
  6. // New file; ask user for save location.
  7. FileChooser chooser = new FileChooser();
  8. // (set FileChooser's title and filters here)
  9. File file = chooser.showSaveDialog(stage);
  10. if (file != null) {
  11. // (save file as CSV or XML depending on chosen filter)
  12. this.currentFile = file;
  13. }
  14. }
  15. });

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")

  1. loadCSV.setOnAction(e -> {
  2. ...
  3. File f = fileChooser.showOpenDialog(null);
  4. loadCSV.getProperties().put("FILE_LOCATION", f.getAbsolutePath());
  5. loadXML.getProperties().remove("FILE_LOCATION");
  6. });

然后我检查哪个变量已填充,然后运行相应的方法。

  1. save.setOnAction(e -> {
  2. try {
  3. if (loadCSV.getProperties().isEmpty() == false) {
  4. File saveCSV = new File(loadCSV.getProperties().get("FILE_LOCATION").toString());
  5. if (saveCSV != null) {
  6. CSV.writeCSV(saveCSV, DataHandler.INSTANCE.foodlist());
  7. }
  8. } else if (loadXML.getProperties().isEmpty() == false) {
  9. File saveXML = new File(loadXML.getProperties().get("FILE_LOCATION").toString());
  10. if (saveXML != null) {
  11. XML.writeXMLList(saveXML, DataHandler.INSTANCE.foodlist());
  12. }
  13. }
  14. });
英文:

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")

  1. loadCSV.setOnAction(e -> {
  2. ...
  3. File f = fileChooser.showOpenDialog(null);
  4. loadCSV.getProperties().put("FILE_LOCATION", f.getAbsolutePath());
  5. loadXML.getProperties().remove("FILE_LOCATION");
  6. });

Then I check which variable is filled and then run the respective method.

  1. save.setOnAction(e -> {
  2. try {
  3. if (loadCSV.getProperties().isEmpty() == false) {
  4. File saveCSV = new File(loadCSV.getProperties().get("FILE_LOCATION").toString());
  5. if (saveCSV != null) {
  6. CSV.writeCSV(saveCSV, DataHandler.INSTANCE.foodlist());
  7. }
  8. } else if (loadXML.getProperties().isEmpty() == false) {
  9. File saveXML = new File(loadXML.getProperties().get("FILE_LOCATION").toString());
  10. if (saveXML != null) {
  11. XML.writeXMLList(saveXML, DataHandler.INSTANCE.foodlist());
  12. }
  13. }
  14. });

huangapple
  • 本文由 发表于 2020年7月25日 16:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63086335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定