我正在写一个Android的应用程序。有两项活动,一项是TextEdit,用来输入'hello message',另一项是按钮来保存内部存储器中的信息。其次是主要活动。你好消息应该在应用程序启动后出现。FileInputStream.read中的IO异常。 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();
我得到IO异常在logcat中的行:
while ((length = fis.read(buffer)) != -1)
,但应用程序似乎工作正确(在应用程序启动后出现定义的消息)。我试图找到解释,我发现了几个主题,但都是根据大文件或资产文件或压缩文件。 我想我的名字文件中像
static String FILENAME = "message_file.zip",
static String FILENAME = "message_file.txt",
尝试不同的扩展,但总是我得到同样的IO异常。
感谢您的建议。
我想使用内部存储,而不是外部。我的文件存在,openFileOutput(String name,int mode)创建文件,如果不存在。我在'while((length = fis.read(buffer))!= -1)'中得到IO异常,而不是FileNotFoundException。 –