2016-03-21 5 views
0

私は、config.propertiesファイルを使用して、サーバーURLを保存しようとしています。アンドロイドの.propertiesファイルに書き込めません

ファイルから読み込みに成功しましたが、ファイルを編集できるようにする必要があります。

これまでのところ、私は可能な解決策を探していましたが、運はありませんでした。どのようなプログラミング言語でもこのようなことをやっていることは初めてです。

これは私が自分の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ファイルで元の値に戻ります。

誰でもこの必然的に必要なコードをどうすればいいのか分かりますか?

+0

これを達成することも可能でしょうか?私はSharedPreferencesについて読んでいます。これは、アプリケーションがデバイスにインストールされている期間全体にわたって永続的なものですか? – Madgarr

+1

SharedPreferencesが必要なもののようです。もちろん、すべてのユーザーは、アプリをアンインストールせずにアプリに属する​​すべてのデータを削除する権利がありますが、これはすべての種類のストレージに適用されます。そして、この場合、彼/彼女はその結果を知っていることを望むことができます。 – 0X0nosugar

+0

@ 0X0nosugarすぐに応答していただきありがとうございます。私は今SharedPreferencesを使用しようとしています。効率的に使用する方法を見つけたら、この質問に答えを出します。このアプリは仕事の人のために使用されるので、彼らは彼らの仕事のために重要な情報を削除するほど愚かではないことを願っています。 – Madgarr

答えて

0

「資産」フォルダ内のプロパティファイルへの書き込みが不可能であったことが判明しました。

私は代わりに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); 
    } 
}