Java FileInputStream Copy copyFileToStream(File file, OutputStream stream)

Here you can find the source of copyFileToStream(File file, OutputStream stream)

Description

Copies the data from file into the stream.

License

BSD License

Parameter

Parameter Description
file a parameter
stream a parameter

Exception

Parameter Description

Declaration

public static void copyFileToStream(File file, OutputStream stream) throws IOException 

Method Source Code

//package com.java2s;
// BSD License (http://www.galagosearch.org/license)

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

import java.io.IOException;

import java.io.OutputStream;

public class Main {
    /**/* w  w  w  .j  a v a2  s.c  o m*/
     * Copies the data from file into the stream.  Note that this method
     * does not close the stream (in case you want to put more in it).
     * 
     * @param file
     * @param stream
     * @throws java.io.IOException
     */
    public static void copyFileToStream(File file, OutputStream stream) throws IOException {
        FileInputStream input = new FileInputStream(file);
        long longLength = file.length();
        final int fiveMegabytes = 5 * 1024 * 1024;

        while (longLength > 0) {
            int chunk = (int) Math.min(longLength, fiveMegabytes);
            byte[] data = new byte[chunk];
            input.read(data, 0, chunk);
            stream.write(data, 0, chunk);
            longLength -= chunk;
        }

        input.close();
    }
}

Related

  1. copyFileToFile(File file, File destFile, boolean overwrite)
  2. copyFileToFile(File input, String outputPath, String exportName)
  3. copyFileToOutputStream(File file, OutputStream stream)
  4. copyFileToOutputStream(String fileLocation, OutputStream os)
  5. copyFileToStream(File currentFile, OutputStream outputStream)
  6. copyFileToStream(File file, OutputStream stream)
  7. copyFileToString(java.io.File f)
  8. copyFileToZip(final ZipOutputStream zos, final byte[] readBuffer, final File file, final int bytesInOfZip)
  9. copyFileUsingFileStreams(final File source, final File dest)