I'm trying to store my Properties-Object and load it again.
There is no exception on storing, but when I try to load and read it, it seems to be empty.
Here you have the code i'm using to store:
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());
}
Here you have the code I'm using to load (for Dev-reasons I execute it directly after storing):
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
When I execute the code I get these Logs:
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
So as you can see, the loaded Property is empty. I have no idea why.
I would expect something like this instead of the error:
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
It doesn't work, and was not able to find a compareable problem here.
If you like to copy and paste my code to androidStudio, please do the necessary Imports (auto-gen) and add a "assets"-folder with a "config.properties"-file to your Project-directory. Hope you guys can help me!
EDIT: I`ve placed a config.properties File with content in the assets-Folder. Now Loading works fine, but I recive this error on storing:
java.io.IOException: write failed: EBADF (Bad file descriptor)