【发布时间】:2011-06-26 06:02:52
【问题描述】:
我很困惑。阅读this thread 后,我正在尝试将列表写入内部存储onPause(),然后读取列表onResume()。仅出于测试目的,我正在尝试编写一个简单的字符串,如示例所示。我有这个要写的:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = null;
try {
fos = openFileOutput(FILENAME, Context. MODE_WORLD_READABLE);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fos.write(string.getBytes());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
我正在尝试通过调用onResume() 阅读它:
try {
resultText01.setText(readFile("hello_file"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
resultText01.setText("exception, what went wrong? why doesn't this text say hello world!?");
}
当然会使用这个:
private static String readFile(String path) throws IOException {
FileInputStream stream = new FileInputStream(new File(path));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
return Charset.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
}
由于某种原因,resultText01.setText(...) 没有设置为"hello world!",而是调用了 catch 异常。如果我的术语不正确,我很抱歉,我是新手。感谢您的任何指点。
【问题讨论】:
标签: android list storage fileinputstream fileoutputstream