写入文件时出现 IOException(流已关闭)

发布于 2024-11-14 11:21:35 字数 1700 浏览 7 评论 0原文

为了向 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑着哭最痛 2024-11-21 11:21:35

看起来服务器已将数据作为压缩流发送。打开流的行和缓存管理器之间没有任何代码吗?

我猜想您还有其他一些代码行正在读取该流,这就是为什么当您到达缓存管理器时它会被关闭。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文