Java BufferedInputStream Copy copyFileToStream(File input, OutputStream os)

Here you can find the source of copyFileToStream(File input, OutputStream os)

Description

copy File To Stream

License

Apache License

Declaration

public static void copyFileToStream(File input, OutputStream os) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void copyFileToStream(File input, OutputStream os) throws IOException {
        InputStream is = new BufferedInputStream(new FileInputStream(input));
        copyStreamToStream(is, os);//from  w  w  w.java  2  s  .  c o m
        is.close();
    }

    /**
     * Copy contents of an input stream to an output stream.  Leave both 
     * streams open afterwards.
     * @param is input stream
     * @param os output stream
     * @throws IOException
     */
    public static void copyStreamToStream(InputStream is, OutputStream os) throws IOException {
        byte[] buffer = new byte[4096];
        for (int read = is.read(buffer); read != -1; read = is.read(buffer)) {
            os.write(buffer, 0, read);
        }
        os.flush();
    }
}

Related

  1. copyFileFromStream(InputStream in, File dest)
  2. copyFileFromZipToDir(String zipFile, String fileNamePattern, File dir)
  3. copyFileNormal(File copyFrom, File copyTo)
  4. copyFileToFile(File in, File out)
  5. copyFileToStream(File from, OutputStream out)
  6. copyFileToTemp(InputStream is, String dirName, String fileName)
  7. copyFileUnique(File in, File outDir)