この質問が以前に聞かれたかどうか教えてください。私のアンドロイドアプリケーションで特定の行のプロパティファイルに書き込む
ゴール
ユーザーは、それが最初Login.classをロードするアプリケーションを起動したとき。
username=
user_hash=
saved_email=
email_hash=
これらのフィールドがされていますapp_prefs.propと呼ばれる(アプリケーションの含まれるファイルのパスで)ローカルファイルが(それがないもの)が存在し、それは次のフィールドがそうのような構造チェックするかどうかを確認するために、このクラスをチェックデフォルトでは空白です。私は次のコードを使用してそれらを読んでいます:
public static String getConfigValue(Context context, String name) {
Resources resources = context.getResources();
String TAG = "Retrieve";
try {
InputStream rawResource = resources.openRawResource(R.raw.app_prefs);
Properties properties = new Properties();
properties.load(rawResource);
return properties.getProperty(name);
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Unable to find the config file: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "Failed to open config file.");
}
return null;
}
デフォルトでは空の値が返されると、ログイン画面が表示されます。そうでない場合は、デフォルトでログインが試行され、成功した場合はアプリケーションに進みます。そうでない場合は、ログインが再度表示されます。
問題
彼らはサインインすると、私はそれらのフィールドにデータを書きたいです。現在、JSONを使用してサーバーとの間で送受信されています。私はこのデータを文字列変数にも抽出することができます。この変数は、ユーザーをログインした後、次のApp画面に進む前に、設定ファイルに保存します。これはどこに問題がある、私は、私もそれにすべての値を渡した許可
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
を有効にしているが、彼らは、私はあまりにもそれらをしたい行に書かれていないです。しかし、その私は私のコンソールにメッセージWrote the values to the stored file
を取得してもカントーファイルにこれらの値を格納していない
private void commitUserInfotoFile(Context context, String username, String passhash, String rmemail, String rmemailhash) {
Resources resources = context.getResources();
String TAG = "Store";
try {
InputStream rawResource = resources.openRawResource(R.raw.app_prefs);
Properties properties = new Properties();
properties.load(rawResource);
//tried using setProperty as well as put but neither works
properties.setProperty("username", username);
properties.put("username", username);
properties.setProperty("user_hash", passhash);
properties.setProperty("saved_email", rmemail);
properties.setProperty("email_hash", rmemailhash);
Log.e(TAG, "Wrote the values to the stored file");
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Unable to find the config file: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IO Exception loading file.");
}
}
:ここで私が使用してファイルを書き込むために使用していたコードです。私は少しこの方法を使用して任意のヘルプが歓迎されるので、プロパティの書き込みに混乱しています。ありがとう
私はストアを使用しようとしましたが、私は 'OutputStream'が定義されていないことがわかりました。手順がわかりません。 – Kaboom
さらに調べると、このファイルをリソースから読んでいることが分かります。リソースは実行時に読み取り専用に書き込むことはできません。結果をアプリストレージに書き込むか、[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences.html)を使用して(推奨) – amacf