写入文件时出现 IOException(流已关闭)
为了向 Android 应用程序添加某种缓存,我尝试将从 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
它是什么,已关闭?
有什么想法吗?
谢谢!
In order to add some sort of Caching to an Android application, I am trying to write the InputStream
that I get from myUrl.openConnection().getInputStream()
, to a file.
This is the method that I wrote:
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();
}
}
}
}
The calling part looks like:
URL feedUrl = new URL("rss_feed_url");
InputStream inputStream = feedUrl.openConnection().getInputSream();
CacheManager.saveInputSteam(inputStream);
I am getting the following exceptions:
(23704): Pas de Notification
W/System.err(24015): java.io.IOException: Stream is closed
What is it, that is closed?
Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来服务器已将数据作为压缩流发送。打开流的行和缓存管理器之间没有任何代码吗?
我猜想您还有其他一些代码行正在读取该流,这就是为什么当您到达缓存管理器时它会被关闭。
Looks like the server has sent the data as a compressed stream. Isn't there any code between the line that is opening the stream, and your cache manager?
I would guess that you have some other lines of code that is reading that stream, and that's why it's closed when you get to the cache manager.