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

Exception

Parameter Description

Declaration

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

Method Source Code


//package com.java2s;
// BSD License (http://lemurproject.org/galago-license)

import java.io.*;

public class Main {
    /**/*from w ww  .  ja  v a2  s.c om*/
     * 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).
     *
     * @throws java.io.IOException
     */
    public static void copyFileToStream(File file, OutputStream stream) throws IOException {
        InputStream input = null;
        try {
            input = new FileInputStream(file);
            copyStream(input, stream);
        } finally {
            if (input != null)
                input.close();
        }
    }

    /**
     * Copies data from the input stream to the output stream.
     *
     * @param input The input stream.
     * @param output The output stream.
     * @throws java.io.IOException
     */
    public static void copyStream(InputStream input, OutputStream output) throws IOException {
        byte[] data = new byte[65536];
        while (true) {
            int bytesRead = input.read(data);
            if (bytesRead < 0) {
                break;
            }
            output.write(data, 0, bytesRead);
        }
    }
}

Related

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