输入-Android-将输入流存储在fi中

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

我面临的问题是我可以创建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();

关于如何保存输入流的任何想法,以便以后可以加载。

干杯

Dobes asked 2020-02-16T02:48:35Z
6个解决方案
92 votes

在这里,输入的是您的inputStream。然后使用相同的File(名称)和FileInputStream将来读取数据。

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();
}
Volodymyr Lykhonis answered 2020-02-16T02:48:48Z
30 votes

简单功能

尝试使用以下简单函数将其整齐地包装在:

// 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 Jordan的回答。

Joshua Pinter answered 2020-02-16T02:49:16Z
9 votes

Kotlin版本(经过测试,无需库):

fun copyStreamToFile(inputStream: InputStream, outputFile: File) {
    inputStream.use { input ->
        val outputStream = FileOutputStream(outputFile)
        outputStream.use { output ->
            val buffer = ByteArray(4 * 1024) // buffer size
            while (true) {
                val byteCount = input.read(buffer)
                if (byteCount < 0) break
                output.write(buffer, 0, byteCount)
            }
            output.flush()
        }
    }
}

我们利用use函数的功能,该函数将在最后自动关闭两个流。

即使发生异常,也可以正确关闭流。

[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html]
[https://kotlinlang.org/docs/tutorials/kotlin-for-py/scoped-resource-usage.html]

vovahost answered 2020-02-16T02:49:54Z
7 votes

较短的版本:

OutputStream out = new FileOutputStream(file);
fos.write(IOUtils.read(in));
out.close();
in.close();
Tuan Chau answered 2020-02-16T02:50:14Z
5 votes

这是一个处理所有异常的解决方案,它基于先前的答案:

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();
        }
    }
}
vovahost answered 2020-02-16T02:50:34Z
0 votes

有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;
}
android developer answered 2020-02-16T02:50:58Z
translate from https://stackoverflow.com:/questions/10854211/android-store-inputstream-in-file