为了向Android应用程序添加某种缓存,我试图将InputStream
(我从myUrl.openConnection().getInputStream()
获得)写入文件。写入文件时发生IOException(流关闭)
这是我写的方法:
public static void saveInputStream(InputStream inputStream) throws IOException, Exception {
FileOutputStream out = null;
OutputStream os = null;
try {
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "feedData.txt";
File myFile = new File(baseDir + File.separator + fileName);
out = new FileOutputStream(myFile, false);
os = new BufferedOutputStream(out);
byte[] buffer = new byte[65536];
int byteRead = 0;
while ((byteRead = inputStream.read(buffer)) != -1) {
Log.d("CacheManager", "Reading.......");
os.write(buffer, 0, byteRead);
}
} catch(IOException e) {
e.printStackTrace();
throw new IOException();
} catch(Exception e) {
throw new Exception();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new IOException();
}
}
}
}
主叫部分看起来像:
URL feedUrl = new URL("rss_feed_url");
InputStream inputStream = feedUrl.openConnection().getInputSream();
CacheManager.saveInputSteam(inputStream);
我收到以下情况除外:
(23704): Pas de Notification
W/System.err(24015): java.io.IOException: Stream is closed
这是什么,那是封闭的?
任何想法?
谢谢!
的确,你猜对了!谢谢,问题解决了! – 2011-06-08 13:41:02