爲了向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