我正在从网址检索XML Feed然后解析它。我需要做的是将内部存储到手机中,这样当没有互联网连接时,它可以解析保存的选项,而不是现场选项。
我面临的问题是我可以创建url对象,使用getInputStream来获取内容,但它不会让我保存它。
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();
我可以如何保存输入流,以便稍后加载。
干杯
答案 0 :(得分:89)
在这里,输入是你的inputStreamReader
。然后使用相同的文件(名称)和FileInputStream
来读取将来的数据。
try {
File file = new File(getCacheDir(), "cacheFileAppeal.srl");
OutputStream output = new FileOutputStream(file);
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();
}
} finally {
input.close();
}
答案 1 :(得分:30)
尝试使用这个简单的功能整齐地将其包裹起来:
// Copy an InputStream to a File.
//
private void copyInputStreamToFile(InputStream in, File file) {
OutputStream out = null;
try {
out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Ensure that the InputStreams are closed even if there's an exception.
try {
if ( out != null ) {
out.close();
}
// If you want to close the "in" InputStream yourself then remove this
// from here but ensure that you close it yourself eventually.
in.close();
}
catch ( IOException e ) {
e.printStackTrace();
}
}
}
感谢Jordan LaPrise和他的answer。
答案 2 :(得分:7)
较短的版本:
OutputStream out = new FileOutputStream(file);
fos.write(IOUtils.read(in));
out.close();
in.close();
答案 3 :(得分:4)
这是一个处理所有异常的解决方案,它基于以前的答案:
void writeStreamToFile(InputStream input, File file) {
try {
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();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 4 :(得分:2)
Kotlin 版本(经过测试,不需要库):
fun copyStreamToFile(inputStream: InputStream, outputFile: File) {
inputStream.use { inputStream ->
val output = FileOutputStream(outputFile)
output.use { outputStream ->
val buffer = ByteArray(4 * 1024) // buffer size
while (true) {
val byteCount = inputStream.read(buffer)
if (byteCount < 0) break
outputStream.write(buffer, 0, byteCount)
}
outputStream.flush()
}
}
}
我们使用use
函数将在最后自动关闭两个流。
答案 5 :(得分:1)
build.gradle
文件中,添加到 dependencies
下: implementation 'commons-io:commons-io:2.5'
import org.apache.commons.io.FileUtils;
// given you have a stream, e.g.
InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
// you can now write it to a file with
FileUtils.copyToFile(inputStream, new File("myfile.txt"));
答案 6 :(得分:0)
有IOUtils的方式:
copy(InputStream input, OutputStream output)
它的代码类似于:
public static long copyStream(InputStream input, OutputStream output) throws IOException {
long count = 0L;
byte[] buffer = new byte[4096];
for (int n; -1 != (n = input.read(buffer)); count += (long) n)
output.write(buffer, 0, n);
return count;
}