gpt4 book ai didi

java - 无法存储和加载属性

转载 作者:行者123 更新时间:2023-12-02 12:14:26 26 4
gpt4 key购买 nike

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

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

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

如您所见,加载的属性是空的。我不知道为什么。
我希望出现这样的结果而不是错误:

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,请执行必要的导入(自动生成)并将带有“config.properties”文件的“assets”文件夹添加到您的项目目录中。希望大家能够帮助我!

编辑:我已在 Assets 文件夹中放置了一个包含内容的 config.properties 文件。现在加载工作正常,但我在存储时收到此错误:

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

最佳答案

  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/

关于java - 无法存储和加载属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46301512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com