简体   繁体   English

无法在android中写入.properties文件

[英]Cannot write in .properties file in android

I have been trying to use a config.properties file to save the server url and such for an app I am making. 我一直在尝试使用config.properties文件保存服务器URL,例如我正在制作的应用程序。

I have succeeded in reading from the file and making it appear but I need the user to be able to edit the file. 我已成功读取文件并使其显示,但我需要用户能够编辑该文件。

So far I have been looking for possible solutions but no luck. 到目前为止,我一直在寻找可能的解决方案,但没有运气。 This is the first time ever doing something of this sort, in any programming language so any help would be appreciated! 这是有史以来第一次使用任何编程语言进行这种操作,因此将不胜感激!

This is the code I have in my SettingsDialog (Where I want the user to be able to modify the settings). 这是我在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);
    }
}

I tried using prop.store while writing this answer, but as soon as my app restarts, it goes back to the original value in the config.properties file. 我在编写此答案时尝试使用prop.store,但是一旦我的应用重新启动,它就会恢复为config.properties文件中的原始值。

Anyone know what to do with this desperately needed piece of code? 有人知道如何处理这个急需的代码吗?

After doing some more reading on the matter, it turned out (or at least that's what I got out of it) that writing in a properties file in the "assets" folder was impossible. 在对该问题进行了更多阅读之后,发现(或者至少是我从中得到的)在“ assets”文件夹中的属性文件中写入是不可能的。

I used SharedPreferences in stead and with way less work, it does what it is supposed to do. 我用SharedPreferences代替了它,并且减少了工作量,可以完成它应该做的事情。

This is the code I have now using 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM