Android AssetManager,写入属性文件

时间:2016-02-09 17:46:09

标签: android

这是我的第一个Android应用程序,所以我对它很新。我能够从属性文件中读取,但在同一活动的保存按钮上回写时遇到问题。

这是我的代码

公共类ConfigurationActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_configuration);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final EditText abc = (EditText) findViewById(R.id.abc);
    ...
...
...


//Reading into a activity and displaying it into the user
    try {
        Properties props = new Properties();;

        InputStream inputStream = getApplicationContext().getAssets().open("app.properties");
        props.load(inputStream);

        abc.setText(props.getProperty("abc"));
        ...
    ...
    ...

        inputStream.close();

    } catch (FileNotFoundException ex) {
        Log.e("Exception", "File donot exists");
    } catch (IOException ex) {
        Log.e("Exception","IO error");
    }

//On the save button I am trying to write back to properties file
    Button save = (Button) findViewById(R.id.SaveButton);
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                Properties props = new Properties();

                props.setProperty("abc", abc.getText().toString());
                ...
        ...
        ...

                props.store(getApplicationContext().getAssets().openFd("app.properties").createOutputStream(), null);

                Intent intent = new Intent(ConfigurationActivity.this, MainActivity.class);
                startActivity(intent);
                finish();

                //inputStream.close();
            } catch (FileNotFoundException ex) {
                Log.e("Exception", "File donot exists");
            } catch (IOException ex) {
                Log.e("Exception","IO error");
            }
        }
    });

}

}

现在我从属性文件中获取数据并将其显示给用户,但在保存按钮上显示FileNotFoundException。我在stackoverflow上寻找其他代码,但我无法解决我的问题 有人可以指导我吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

您无法在运行时修改资产或资源。它们是只读的。