Write data to .properties file in assets directiry

2019-07-05 07:50发布

问题:

I have "config.properties" in my assets folder and I try to get and write data to this file. When I try to get data (getProperty()) - everything is ok, but when to write (setProperty()) - I get "java.io.IOException: write failed: EBADF (Bad file descriptor)" in my log and data in my "config.properties" does not change. Here is my class:

class Property {

private static Properties properties = new Properties();

static String getProperty(String key, Context context) throws IOException {
    AssetManager assetManager = context.getAssets();
    InputStream inputStream = assetManager.open("config.properties");
    properties.load(inputStream);
    return properties.getProperty(key);
}

static void setProperty(String ipAddress, Context context) {
    try {
        properties.setProperty("ip_address", ipAddress);
        properties.store(context.getAssets().openFd("config.properties").createOutputStream(), null);
    }catch (Exception e){
        e.printStackTrace();
    }
}
}

回答1:

I have "config.properties" in my assets folder and I try to get and write data to this file

That is not possible. You can read assets at runtime, but you cannot modify them.