【发布时间】:2015-05-13 07:21:06
【问题描述】:
我能够在 SAME 活动中写入然后读取文本文件,但在从另一个 Activity 写入文本文件后我无法读取该文件。
例如: Activity A 创建并写入文本文件。 活动 B 读取该文本文件。
我使用此代码写入 Activity A 中的文本文件:
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try
{
fos = openFileOutput("user_info.txt", Context.MODE_WORLD_WRITEABLE);
osw = new OutputStreamWriter(fos);
osw.write("text here");
osw.close();
fos.close();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后我使用此代码尝试读取 Activity A 创建的相同文本文件,但我得到了 FileNotFoundException:
try
{
FileInputStream fis = openFileInput("user_info.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader buff = new BufferedReader(isr);
String line;
while((line = buff.readLine()) != null)
{
Toast.makeText(this, line, Toast.LENGTH_LONG).show();
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
有人知道我为什么收到FileNotFoundException 吗?
是路径问题吗?
【问题讨论】:
标签: android file read-write