Android InputStream Save writeFile(File file, InputStream inStream)

Here you can find the source of writeFile(File file, InputStream inStream)

Description

write File

Declaration

public static int writeFile(File file, InputStream inStream)
            throws IOException 

Method Source Code

//package com.java2s;

import java.io.*;

public class Main {
    private final static int BUFFER = 8192;

    public static void writeFile(File file, byte[] data) throws Exception {
        DataOutputStream out = null;
        if (null != file && null != data) {
            try {
                out = new DataOutputStream(new FileOutputStream(file));
                out.write(data);//w  w  w.  j a  v  a  2 s.  c  o  m
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }
    }

    public static int writeFile(File file, InputStream inStream)
            throws IOException {
        long dataSize = 0;
        DataInputStream in = null;
        DataOutputStream out = null;
        if (null != inStream && null != file) {
            try {
                byte buffer[] = new byte[BUFFER];
                out = new DataOutputStream(new FileOutputStream(file));
                in = new DataInputStream(inStream);

                int nbyteread;
                while ((nbyteread = in.read(buffer)) != -1) {
                    out.write(buffer, 0, nbyteread);
                    dataSize += nbyteread;
                }
            } finally {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            }
        }

        return (int) (dataSize / 1024);
    }
}

Related

  1. saveStream(InputStream is, String savePath)
  2. saveTmpFile(InputStream is)
  3. toBase64OutputStream(InputStream is, OutputStream os)
  4. toOutputStream(InputStream is, OutputStream os)
  5. doOutputFile(InputStream is, String filename)