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

时间:2020-07-25 08:51:44

标签: java file

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

此条目应使用更改后的数据覆盖上次加载的文件,而无需再次打开对话框(例如在MS Excel或Word中)。

我当然已经做过一些研究,但是没有找到合适的东西或没有找到错误的东西。

我已经拥有的这些条目和功能可以正常工作:

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
});

我要创建此条目:

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

This is my application

2 个答案:

答案 0 :(得分:0)

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

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:cardCornerRadius="@dimen/loginCardRadius"
android:elevation="5dp"
android:layout_gravity="center"
android:layout_marginTop="@dimen/loginViewsMargin"
android:layout_marginBottom="@dimen/loginViewsMargin"
android:background="#ffffff">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:padding="@dimen/loginViewsMargin">

    <ImageView
        android:contentDescription="@string/string1"
        android:src="@drawable/logo"
        android:layout_gravity="center"
        android:layout_height="100dp"
        android:layout_width="wrap_content"
        android:layout_marginTop="@dimen/loginViewsMargin"/>

    <GridView
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/grid_view"
        android:numColumns="2"/>

</LinearLayout>
</androidx.cardview.widget.CardView>

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="wrap_content">
<ScrollView
    android:fillViewport="true"
    android:layout_height="match_parent"
    android:layout_width="wrap_content">

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_alignParentTop="true"
            android:baselineAligned="true"
            android:weightSum="12"
            android:background="@drawable/login_shape_bk"
            android:orientation="vertical"
            android:layout_weight="3">


            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_login_bk" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:orientation="vertical"
            android:layout_marginTop="40dp"
            android:layout_marginRight="30dp"
            android:layout_marginLeft="30dp">

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:textColor="#ffffff"
                android:textSize="22sp"
                android:textStyle="bold"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:text="@string/research_work"/>

            <include
                layout="@layout/layout_advisors"/>

        </LinearLayout>

    </RelativeLayout>

</ScrollView>

现在,您可以使用该private File currentFile; 值进行保存:

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;
    }
});

如果您尚未将保存逻辑转移到私有方法中,则可能要这么做。

答案 1 :(得分:0)

我有一个适合我的解决方案。它可能不是最佳解决方案,但仍然可能有用。

我将一个属性传递给包含我文件 .put(“ FILE_LOCATION”,..)的路径的menuItem,并在保存事件中使用此属性来选择最后一个路径。 / p>

由于我还想自动另存为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());
                }
            }
    });