私はAndroidのアプリを書いています。 2つのアクティビティ、1つは「hello message」と入力するTextEdit、内部ストレージにメッセージを保存するボタンです。第二は主な活動です。アプリ起動後にHelloメッセージが表示されます。IOはFileInputStream.readの例外です。 Android
第二アクティビティ:
String s = ((EditText) findViewById(R.id.message_act_editText_hello)).getText().toString();
FileOutputStream fos = openFileOutput(Lab2AndroidActivity.FILENAME, Context.MODE_PRIVATE);
fos.write(s.getBytes());
fos.close();
最初の(メイン)活動:
static String FILENAME = "message_file.zip";
FileOutputStream fos;
try {
//piece of code to guarantee that file exists
fos = openFileOutput(Lab2AndroidActivity.FILENAME, Context.MODE_APPEND);
fos.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis = openFileInput(FILENAME);
messageString = new StringBuffer("");
while ((length = fis.read(buffer)) != -1) {
String temp = new String(buffer, 0,length);
messageString.append(temp);
fis.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast t = Toast.makeText(this, messageString, 3000);
t.show();
私はラインでlogcatにIO例外を取得しています:
while ((length = fis.read(buffer)) != -1)
が、アプリが動作しているようです正しく(アプリケーションの開始後に定義されたメッセージが表示されます)。私は説明を見つけようとしましたが、いくつかのトピックが見つかりましたが、すべて大容量のファイル、資産のファイル、または圧縮ファイルに従っていました。 私は別の拡張機能を試して
static String FILENAME = "message_file.zip",
static String FILENAME = "message_file.txt",
ように私のファイルに名前を付けてみましたが、常に私は同じIO例外を取得しています。
ありがとうございます。当然の
私は内部ストレージを使用したい、外部ではない。私のファイルが存在し、openFileOutput(文字列名、intモード)が存在しない場合、ファイルを作成します。私はIO例外を取得していますが、 'while((length = fis.read(buffer))!= -1)'のFileNotFoundExceptionではありません。 –