Java Android studio,重新打开应用程序后删除File.properties

Java Android studio,重新打开应用程序后删除File.properties,java,android,file,Java,Android,File,我在this.getFilesDir()+“Data.property”中保存了一个File.properties文件。 在应用程序中,我保存了用户编写的数据,但当我打开应用程序时,我上次保存的所有数据(或文件)都已被删除 例如: // Store for (Map.Entry<String,String> entry : MainActivity.Notes.entrySet()) { // Put all data from Notes in pr

我在
this.getFilesDir()+“Data.property”
中保存了一个File.properties文件。 在应用程序中,我保存了用户编写的数据,但当我打开应用程序时,我上次保存的所有数据(或文件)都已被删除

例如:

        // Store
        for (Map.Entry<String,String> entry : MainActivity.Notes.entrySet()) { // Put all data from Notes in properties to store later
            properties.put(entry.getKey(), entry.getValue());
        }

        try { properties.store(new FileOutputStream(this.getFilesDir() + "data.properties"), null); } // Store the data
        catch (IOException e) { e.printStackTrace(); } // Error exception

        // Load the map
        Properties properties = new Properties(); // Crate properties object to store the data


        try {
            properties.load(new FileInputStream(this.getFilesDir() + "data.proprties")); } // Try to load the map from the file
        catch (IOException e) { e.printStackTrace(); } // Error exception

        for (String key : properties.stringPropertyNames()) {
            Notes.put(key, properties.get(key).toString()); // Put all data from properties in the Notes map
        }

// Can load the data

//存储
对于(Map.Entry:MainActivity.Notes.entrySet()){//将Notes中的所有数据放在属性中,以便以后存储
properties.put(entry.getKey(),entry.getValue());
}
尝试{properties.store(newfileoutputstream(this.getFilesDir()+“data.properties”),null);}//存储数据
catch(IOException e){e.printStackTrace();}//错误异常
//加载地图
属性属性=新属性();//用于存储数据的板条箱属性对象
试一试{
properties.load(新文件输入流(this.getFilesDir()+“data.properties”);}//尝试从文件中加载映射
catch(IOException e){e.printStackTrace();}//错误异常
for(字符串键:properties.stringPropertyNames()){
Notes.put(key,properties.get(key.toString());//将属性中的所有数据放入Notes映射
}
//可以加载数据
你可以看到我在文件中保存了数据,我可以加载它,但当我打开应用程序时,数据已被删除

有一种方法可以写入文件并保存下次打开应用程序时写入的数据?首先返回一个
文件
对象。此
文件
表示应用程序的私有数据目录

然后执行
getFilesDir()+“foo”
,它将隐式地调用
File
实例上的
toString()
。相当于
File.getPath
,它不一定返回尾部斜杠

这意味着,如果
getFilesDir()
/data/app
处返回一个
文件
,则属性文件将变为
/data/appdata.properties
,该文件不在数据文件夹中,无法写入

相反,要在目录中获取
文件
,可以使用该目录创建一个新的
文件
实例。e、 g:

File propertiesFile=new文件(this.getFilesDir(),“data.properties”);
//将propertiesFile用于FileOutputStream/FileInputStream等。

这将确保您的属性文件位于该目录中,并防止文件分隔符出现任何问题

Thank's!我已经做了两天了,我正要给up@ZivSion很乐意帮忙!建议始终尝试使用
文件
对象,而不是通过字符串构造路径。大多数类/库无论如何都使用
File
而不是传递路径(FileInputStream具有同时接受这两者的构造函数)。此外,从代码中,您应该在logcat中看到错误/异常。始终帮助您了解这一点,如果您需要有关stackoverflow的进一步帮助,logcat通常非常有用,可以作为信息提供:)