无法存储和加载属性

时间:2017-09-19 13:15:40

标签: java android properties

我正在尝试存储我的属性对象并再次加载它 存储没有异常,但是当我尝试加载和读取它时,它似乎是空的。

这里有你用来存储的代码:

public class mainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //[...]
    private String filename = "config.properties"; //config-savePath
    private HashMap<String, String> someMap= new HashMap<>(); 
    someMap.put("SOME_KEY", "myValue");

    try{  //try to store and log (seems to work)
        Properties properties = new Properties();
        properties.putAll(someMap);

        Log.v("===INFO===","List all Properties:");
        for (String key : properties.stringPropertyNames()) {
                Log.v("===INFO===","Property :" + key + " = " + properties.get(key).toString());
        }

        properties.store(getAssets().openFd(filename).createOutputStream(), null);
        Log.v("===INFO===","storing information successfully");
    }catch (IOException e){
        Toast.makeText(this,"- Error -\n information could not be stored",Toast.LENGTH_SHORT).show();
        Log.v("===ERR===", "Cert-Info could not be stored -fileNotFound-" + e.toString());
    }

在这里你有我用来加载的代码(出于开发原因我直接在存储后执行它):

    try{  //try to load and log
        Log.v("===INFO===","Try loading Properties after storing");
        Properties properties = new Properties();
        properties.load(getBaseContext().getAssets().open(filename,Context.MODE_PRIVATE));

        if (properties.isEmpty()){
            Log.v("===ERR===", "Properties is empty");
        }else{
            for (String key : properties.stringPropertyNames()) {
                Log.v("===INFO===","Property :" + key + " = " + properties.get(key).toString());
            }
        }
    }catch (IOException e){
        Log.v("===ERR===", "Properties could not be loaded" + e.toString());
        e.printStackTrace();
    }
}}//end oncreate //end class

当我执行代码时,我得到了这些日志:

09-19 15:01:18.172 31242-31242/com.example.myapplication V/===INFO===: List all Properties:
09-19 15:01:18.173 31242-31242/com.example.myapplication V/===INFO===: Property :SOME_KEY = myValue
09-19 15:01:18.176 31242-31242/com.example.myapplication V/===INFO===: storing information successfully
09-19 15:01:18.176 31242-31242/com.example.myapplication V/===INFO===: Try loading Properties after storing
09-19 15:01:18.177 31242-31242/com.example.myapplication V/===ERR===: Properties is empty

如您所见,加载的Property为空。我不明白为什么。
我希望这样的事情而不是错误:

09-19 15:01:18.177 31242-31242/com.example.myapplication V/===INFO===: Try loading Properties after storing
09-19 15:01:18.178 31242-31242/com.example.myapplication V/===INFO===: Property :SOME_KEY = myValue

它不起作用,并且无法在此找到可比较的问题。

如果您想将我的代码复制并粘贴到androidStudio,请执行必要的Imports(自动生成)并将“assets”文件夹与“config.properties”文件一起添加到您的Project目录中。 希望你们能帮助我!

编辑:我在assets-Folder中放置了一个包含内容的config.properties文件。现在加载工作正常,但我在存储时发现了这个错误:

java.io.IOException: write failed: EBADF (Bad file descriptor)

2 个答案:

答案 0 :(得分:1)

  HashMap<String, String> mymap = new HashMap<String, String>();

Option 1:
     Properties prop = new Properties();
     prop.setProperty("SOME_KEY", "myValue");


Option2 :
     mymap.put("1", "test");
     mymap.put("2", "test2");
     prop.putAll(mymap);
     prop.store(new FileOutputStream("config.properties"),"");


Please refer to following link
    https://www.mkyong.com/java/java-properties-file-examples/

答案 1 :(得分:0)

经过一些研究,我可以找出问题所在。 它取决于&#34; Asserts&#34; -Folder - 它是一个简单的只读 - 问题。 这里回答:Is Asset folder read-only?

这就是为什么&#34;加载&#34;工作正常,但&#34;存储&#34;才不是。 似乎如果您尝试写入空的.property文件,则不会抛出任何异常,即使它应该(它也不会工作)。

常见的解决方法是使用Internal Storage而不是asserts-folder。