copy file to OutputStream - Java java.io

Java examples for java.io:OutputStream

Description

copy file to OutputStream

Demo Code


//package com.java2s;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {
    public static void copy(String filePath, OutputStream os)
            throws IOException {
        FileInputStream is = null;
        try {// w w  w .  j  a  v a 2 s .  c om
            is = new FileInputStream(filePath);
            byte[] data = new byte[8096];
            int len = -1;
            while ((len = is.read(data)) != -1) {
                os.write(data, 0, len);
            }
        } finally {
            if (is != null)
                try {
                    is.close();
                } catch (IOException e) {
                }
        }
    }
}

Related Tutorials