简体   繁体   中英

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.

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).

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.

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.

I used SharedPreferences in stead and with way less work, it does what it is supposed to do.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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