나는 안드로이드의 애플 리케이션을 writting 해요. 두 가지 활동 중 하나에는 'hello message'를 입력하는 TextEdit과 내부 저장소에 메시지를 저장하는 버튼이 있습니다. 두 번째 주요 활동입니다. 앱 시작 후 Hello 메시지가 표시됩니다.FileInputStream.read의 IO 예외입니다. 안드로이드
두 번째 활동
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();
내가 줄에서 로그 캣에서 IO 예외를 받고 있어요 :
while ((length = fis.read(buffer)) != -1)
하지만 응용 프로그램이 작동하는 것 같다 올바르게 (앱이 시작된 후에 정의 된 메시지가 표시됨) 설명을 찾으려고 노력했지만 몇 가지 주제를 발견했지만 모두 대용량 파일 또는 자산의 파일 또는 압축 파일에 따라 발생했습니다. 나는 다른 확장을 시도
static String FILENAME = "message_file.zip",
static String FILENAME = "message_file.txt",
처럼 내 파일의 이름을했지만, 항상 내가 같은 IO 예외를 얻고있다.
제안 해 주셔서 감사합니다. 물론
내부가 아닌 외부 저장소를 사용하고 싶습니다. 내 파일이 존재하지 않으면 openFileOutput (String name, int mode) 파일이 생성됩니다. 내가 IO 예외를 받고있어, FileNotFoundException이 아니라 'while ((length = fis.read (buffer))! = -1)'. –