Java FileChannel Copy copyFile(FileInputStream fromFile, FileOutputStream toFile)

Here you can find the source of copyFile(FileInputStream fromFile, FileOutputStream toFile)

Description

Creates the specified toFile as a byte for byte copy of the fromFile.

License

Open Source License

Parameter

Parameter Description
fromFile - FileInputStream for the file to copy from.
toFile - FileInputStream for the file to copy to.

Declaration

public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class Main {
    /**/*from   w ww. j  ava2s.c  om*/
     * Creates the specified <code>toFile</code> as a byte for byte copy of the
     * <code>fromFile</code>. If <code>toFile</code> already exists, then it
     * will be replaced with a copy of <code>fromFile</code>. The name and path
     * of <code>toFile</code> will be that of <code>toFile</code>.<br/>
     * <br/>
     * <i> Note: <code>fromFile</code> and <code>toFile</code> will be closed by
     * this function.</i>
     * 
     * @param fromFile
     *            - FileInputStream for the file to copy from.
     * @param toFile
     *            - FileInputStream for the file to copy to.
     */
    public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
        FileChannel fromChannel = null;
        FileChannel toChannel = null;
        try {
            fromChannel = fromFile.getChannel();
            toChannel = toFile.getChannel();
            fromChannel.transferTo(0, fromChannel.size(), toChannel);
        } finally {
            try {
                if (fromChannel != null) {
                    fromChannel.close();
                }
            } finally {
                if (toChannel != null) {
                    toChannel.close();
                }
            }
        }
    }
}

Related

  1. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  2. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  3. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  4. copyFile(File srcFile, File dstFile, boolean overwrite)
  5. copyFile(FileChannel in, FileChannel out)
  6. copyFile(final File in, final File out)
  7. copyFile(final File in, final File out)
  8. copyFile(final File input, final File output)
  9. copyFile(final File src, final File dest)