ファイルの読み書きをしているプログラムです。
このプログラムは、onCreateでファイルからテキストデータがnull判定が出るまで1行づつ読み込み、それを配列に格納してedit textに表示しています。
button1にすすむと、3つのedit textの内容をonCreateで読み込んだ同じファイルに上書きで保存し,ホーム画面に移動するようにしています。
ここで、if文を使い、edit textが何も入力されていないのなら、Toastで警告を出し、すべてfalseで返されたら、ファイルを書き込んでいます。しかし、どれかがtrueになってしまったらなぜかファイルの内容が消えてしまいます。なぜでしょうか?
public class MainActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_main); //button1 ボタンの取得 Button btn1 = (Button) findViewById(R.id.Button_decide); //クリックイベントの通知先指定 btn1.setOnClickListener(new button1CliclListener()); EditText editText1 = (EditText)findViewById(R.id.souti_ID); EditText editText2 = (EditText)findViewById(R.id.password); EditText editText3 = (EditText)findViewById(R.id.URL); // SDカード上のファイルパスと指定のファイル String filepath = Environment.getExternalStorageDirectory() + "/setting.txt"; String[] text = new String[1000]; try { FileInputStream fileInputStream = new FileInputStream(filepath); BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream,"UTF-8")); String str; int i = 0; while ((str = reader.readLine() ) != null) // nullまで1行づつ読み込み { text[i]=str; // 1行づつ配列に格納 i = i +1; } editText1.setText(text[0]); editText2.setText(text[1]); editText3.setText(text[2]); reader.close(); fileInputStream.close(); } catch (FileNotFoundException e) { } catch (IOException e) { } } //button1を押したときの処理(書き込み処理) private class button1CliclListener implements View.OnClickListener { @Override public void onClick(View v){ //ファイルのパスと指定のテキストファイル String filePath = Environment.getExternalStorageDirectory()+ "/setting.txt"; FileOutputStream fos; try { fos = new FileOutputStream(filePath,false); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); BufferedWriter bw = new BufferedWriter(osw); String str1 = ((EditText) findViewById(R.id.souti_ID)).getText().toString(); String str2 = ((EditText) findViewById(R.id.password)).getText().toString(); String str3 = ((EditText) findViewById(R.id.URL)).getText().toString(); if (str1.length() == 0) { Toast.makeText(getApplicationContext(), getString(R.string.error1), Toast.LENGTH_SHORT).show(); } else if (str2.length() == 0) { Toast.makeText(getApplicationContext(), getString(R.string.error2), Toast.LEN GTH_SHORT).show(); } else if (str3.length() == 0) { Toast.makeText(getApplicationContext(),getString(R.string.error3), Toast.LENG TH_SHORT).show(); } else { bw.write(str1); bw.newLine(); bw.write(str2); bw.newLine(); bw.write(str3); bw.flush(); fos.close(); //メッセージ Toast.makeText(getApplicationContext(), "設定を保存しました。", Toast.LENGTH_LONG).show(); // ホーム画面に戻る Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
`}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/13 07:50