Android的 - 存储的InputStream文件(Android - Store inputs

2019-06-25 02:41发布

我retrieveing从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();

任何想法如何,我可以去节省输入流,所以我可以在以后加载它。

干杯

Answer 1:

这是,输入你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();
}


Answer 2:

简单的功能

试试这个简单的功能巧妙地包裹起来的:

// 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();
        }
    }
}

由于乔丹LaPrise和他的答案 。



Answer 3:

一个较短的版本:

OutputStream out = new FileOutputStream(file);
fos.write(IOUtils.read(in));
out.close();
in.close();


Answer 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();
        }
    }
}


Answer 5:

科特林版本(测试并没有图书馆需要):

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功能,将在年底自动关闭两个流。



Answer 6:

有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 - Store inputstream in file