Android OutputStream Write writeFile(InputStream inputStream, OutputStream outputStream)

Here you can find the source of writeFile(InputStream inputStream, OutputStream outputStream)

Description

write File

Declaration

private static void writeFile(InputStream inputStream,
            OutputStream outputStream) throws IOException 

Method Source Code

//package com.java2s;

import java.io.Closeable;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class Main {
    public static void writeFile(String filepath, String text) {
        OutputStreamWriter wr = null;
        try {/*from   w w w .ja va 2s. co m*/
            File file = new File(filepath);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
            }
            wr = new OutputStreamWriter(new FileOutputStream(filepath,
                    false), "UTF8");
            wr.write(text);
            wr.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (wr != null) {
                try {
                    wr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void writeFile(InputStream inputStream,
            OutputStream outputStream) throws IOException {
        final byte[] buf = new byte[10240];
        int len;
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.flush();
        closeIOStream(outputStream);
        closeIOStream(inputStream);
    }

    public static void closeIOStream(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. writeSInt24ToStream(final OutputStream destination, final int value)
  2. copy(InputStream in, OutputStream out)
  3. writeByte(final String str, final OutputStream out, Map cache)
  4. uint32ToStream(long value, OutputStream stream)
  5. uint64ToStream(long value, OutputStream stream)
  6. putLong(OutputStream out, final long value, final int fitInBytes)
  7. outputSubFile(InputStream is, String subFile, String targetPath, String filename)