Java FileChannel Copy copyFiles(final File fromFile, final File toFile)

Here you can find the source of copyFiles(final File fromFile, final File toFile)

Description

Copies the contents of the input file to the output file

License

Open Source License

Parameter

Parameter Description
fromFile a parameter
toFile a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFiles(final File fromFile, final File toFile) throws IOException 

Method Source Code


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

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

public class Main {
    /**//from  w w  w  .ja va 2 s.c  o  m
     * Copies the contents of the input file to the output file
     * @param fromFile
     * @param toFile
     * @throws IOException
     */
    public static void copyFiles(final File fromFile, final File toFile) throws IOException {
        FileInputStream inFile = new FileInputStream(fromFile);
        FileOutputStream outFile = new FileOutputStream(toFile);
        FileChannel inChannel = inFile.getChannel();
        FileChannel outChannel = outFile.getChannel();
        int bytesWritten = 0;
        long byteCount = inChannel.size();
        while (bytesWritten < byteCount) {
            bytesWritten += inChannel.transferTo(bytesWritten, byteCount - bytesWritten, outChannel);
        }
        inFile.close();
        outFile.close();
    }
}

Related

  1. copyFileNio(File src, File dst)
  2. copyFileOrDirectory(File source, File destination, boolean flag)
  3. copyFiles(File originalFile, File destinationFile)
  4. copyFiles(File source, File dest)
  5. copyFiles(File srcFile, File dstFile, boolean overwrite)
  6. copyFileToDir(File file, File destDir)
  7. copyFileToDirectory(String fileFrom, String destinationDirectory)
  8. copyFileToFolder(final String resultFile, final String targetPath)
  9. copyFileToStream(File in, OutputStream out)