Android - 将输入流存储在文件中 [英] Android - Store inputstream in file

查看:28
本文介绍了Android - 将输入流存储在文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 url 检索 XML 提要,然后对其进行解析.我还需要做的是将其存储在手机内部,以便在没有互联网连接时它可以解析保存的选项而不是实时选项.

I am retrieveing an XML feed from a url and then parsing it. What I need to do is also store that internally to the phone so that when there is no internet connection it can parse the saved option rather than the live one.

我面临的问题是我可以创建url对象,使用getInputStream获取内容,但它不会让我保存它.

The problem I am facing is that I can create the url object, use getInputStream to get the contents, but it will not let me save it.

URL url = null;
InputStream inputStreamReader = null;
XmlPullParser xpp = null;

url = new URL("http://*********");
inputStreamReader = getInputStream(url);

ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFileAppeal.srl"));

//--------------------------------------------------------
//This line is where it is erroring.
//--------------------------------------------------------
out.writeObject( inputStreamReader );
//--------------------------------------------------------
out.close();

关于如何保存输入流以便稍后加载的任何想法.

Any ideas how I can go about saving the input stream so I can load it later.

干杯

推荐答案

在这里,输入就是你的 inputStream.然后使用相同的文件(名称)和 FileInputStream 将来读取数据.

Here it is, input is your inputStream. Then use same File (name) and FileInputStream to read the data in future.

try {
    File file = new File(getCacheDir(), "cacheFileAppeal.srl");
    try (OutputStream output = new FileOutputStream(file)) {
        byte[] buffer = new byte[4 * 1024]; // or other buffer size
        int read;

        while ((read = input.read(buffer)) != -1) {
            output.write(buffer, 0, read);
        }

        output.flush();
    }
} finally {
    input.close();
}

这篇关于Android - 将输入流存储在文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆