Android的 - 存储的InputStream文件 [英] Android - Store inputstream in file
问题描述
我retrieveing从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.
干杯 Dobes
推荐答案
这,输入你的 InputStreamReader的
。然后,使用相同的文件(名称)和的FileInputStream
读取数据的未来。
Here it is, input is your inputStreamReader
. Then use same File (name) and FileInputStream
to read the data in future.
try {
File file = new File(getCacheDir(), "cacheFileAppeal.srl");
OutputStream output = new FileOutputStream(file);
try {
try {
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 {
output.close();
}
} catch (Exception e) {
e.printStackTrace(); // handle exception, define IOException and others
}
} finally {
input.close();
}
这篇关于Android的 - 存储的InputStream文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!