Java OutputStream Write copyFile(final InputStream in, final OutputStream out, final int size)

Here you can find the source of copyFile(final InputStream in, final OutputStream out, final int size)

Description

Copy a file from one location to another

License

Open Source License

Parameter

Parameter Description
in Ingoing File
out Outgoing File
size Byte Buffer Size (in bytes)

Declaration

public static void copyFile(final InputStream in, final OutputStream out, final int size) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

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

public class Main {
    /**//from   w ww . j a  va  2  s  .  co  m
     * Copy a file from one location to another
     *
     * @param in
     *            Ingoing File
     * @param out
     *            Outgoing File
     * @param size
     *            Byte Buffer Size (in bytes)
     */
    public static void copyFile(final InputStream in, final OutputStream out, final int size) {
        try {
            final byte[] buffer = new byte[size];
            int length;
            while ((length = in.read(buffer)) > 0)
                out.write(buffer, 0, length);
        } catch (final Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. copyBytesAndClose(final InputStream is, final OutputStream os)
  2. copyBytesToFile(byte[] bytes, File outputFile)
  3. copyBytesToStream(ByteArrayOutputStream from, OutputStream to)
  4. copyBytesToStream(InputStream inputStream, OutputStream outputStream, int length)
  5. copyFile(File fromFile, File toFile)
  6. copyFile(InputStream in, OutputStream out)
  7. copyFile(InputStream in, OutputStream out, boolean close)
  8. copyFile(InputStream inputStream, OutputStream outputStream)
  9. copyFile(OutputStream os, InputStream is)