Java 写入文件时发生IOException(流已关闭)
为了向Android应用程序添加某种缓存,我尝试将从Java 写入文件时发生IOException(流已关闭),java,android,inputstream,ioexception,Java,Android,Inputstream,Ioexception,为了向Android应用程序添加某种缓存,我尝试将从myUrl.openConnection().getInputStream()获取的InputStream写入一个文件 这就是我写的方法: public static void saveInputStream(InputStream inputStream) throws IOException, Exception { FileOutputStream out = null; OutputStream os =
myUrl.openConnection().getInputStream()
获取的InputStream
写入一个文件
这就是我写的方法:
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
是什么,关了吗
有什么想法吗
谢谢 看起来服务器已将数据作为压缩流发送。在打开流的行和缓存管理器之间没有任何代码吗 我猜您还有一些其他代码行正在读取该流,这就是为什么当您到达缓存管理器时它会关闭的原因