[英]Cannot write in .properties file in android
我一直在嘗試使用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文件中的原始值。
有人知道如何處理這個急需的代碼嗎?
在對該問題進行了更多閱讀之后,發現(或者至少是我從中得到的)在“ 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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.