【问题标题】:Cannot write in .properties file in android无法在android中写入.properties文件
【发布时间】:2016-07-08 03:39:55
【问题描述】:

我一直在尝试使用 config.properties 文件来保存服务器 url 以及我正在制作的应用程序。

我已成功读取文件并使其显示,但我需要用户能够编辑该文件。

到目前为止,我一直在寻找可能的解决方案,但没有运气。这是有史以来第一次用任何编程语言做这种事情,所以任何帮助都将不胜感激!

这是我在 SettingsDialog 中的代码(我希望用户能够修改设置)。

public class SettingsDialog extends Dialog {
    Context context;
    SettingsDialog dialog;
    PropertyReader propertyReader;
    Properties prop;
    EditText url;
    public SettingsDialog(Context context){
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_settings_test);

        dialog = this;

        //let's try this
        Resources res = context.getResources();
        AssetManager am = res.getAssets();

        try{
            InputStream is = am.open("config.properties");
            prop = new Properties();
            prop.load(is);
        }catch(IOException e){
            System.err.println("Failed to open config.properties file");
            e.printStackTrace();
        }
        //well what do you know, it worked as well


//        propertyReader = new PropertyReader(context);
//        prop = propertyReader.getMyProperties("config.properties");

        //Elements on Dialog
        EditText port = (EditText) findViewById(R.id.settings_port);
        url = (EditText) findViewById(R.id.settings_url);
        Button btnOk = (Button) findViewById(R.id.settings_ok);
        Button btnCancel = (Button) findViewById(R.id.settings_cancel);

        url.setText(prop.getProperty("server_url").toString());

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prop.setProperty("server_url", url.getText().toString());
                try {
                   prop.store(am.openFd("config.properties").createOutputStream(), null);
                } catch (FileNotFoundException e) {
                   e.printStackTrace();
                } catch (IOException e) {
                   e.printStackTrace();
                }
                Toast.makeText(context, prop.getProperty("server_url"), Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }
        });

        getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    }
}

我在编写此答案时尝试使用 prop.store,但一旦我的应用重新启动,它就会恢复到 config.properties 文件中的原始值。

有人知道如何处理这段急需的代码吗?

【问题讨论】:

  • 我读了一点,这甚至可能实现吗?我阅读了有关 SharedPreferences 的信息,这是否会在应用程序安装在设备上的整个过程中持续存在?
  • SharedPreferences 似乎是您所需要的。当然,每个用户都有权在不卸载应用程序的情况下删除属于应用程序的所有数据,但这适用于所有类型的存储。在这种情况下,人们可以希望他/她知道后果。
  • @0X0nosugar 感谢您的快速回复,我现在正在尝试使用 SharedPreferences,一旦我弄清楚如何有效地使用它们,我就会在这个问题上给出答案。这个应用程序将用于工作人员,所以我们希望他们不会愚蠢到删除他们工作的关键信息哈哈。

标签: java android properties-file


【解决方案1】:

在对此事进行了更多阅读之后,事实证明(或者至少这是我从中得到的)在“assets”文件夹中的属性文件中写入是不可能的。

我改用了SharedPreferences,而且工作量更少,它完成了它应该做的事情。

这是我现在使用 SharedPreferences 的代码:

public class SettingsDialog extends Dialog {
    Context context;
    SettingsDialog dialog;
    EditText url;
    SharedPreferences sp;
    public SettingsDialog(Context context){
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_settings_test);

        dialog = this;

        //using SharedPreferences
        sp = context.getSharedPreferences("mobile_mover", Context.MODE_PRIVATE);

        //Elements on Dialog
        EditText port = (EditText) findViewById(R.id.settings_port);
        url = (EditText) findViewById(R.id.settings_url);
        Button btnOk = (Button) findViewById(R.id.settings_ok);
        Button btnCancel = (Button) findViewById(R.id.settings_cancel);

        url.setText(sp.getString("server_url", "http://www.sharedpreferencesrock.eu"));

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sp.edit().putString("server_url", url.getText().toString()).commit();
                dialog.dismiss();
            }
        });
        getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多