Java FileChannel Copy copyFile(File sourceFile, File destFile)

Here you can find the source of copyFile(File sourceFile, File destFile)

Description

copy File

License

Apache License

Declaration

public static void copyFile(File sourceFile, File destFile) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache 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 {
    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (sourceFile.exists()) {
            System.out.println(sourceFile.getAbsolutePath() + " exists");
            System.out.println("Is directory = " + sourceFile.isDirectory());
            System.out.println("Can read = " + sourceFile.canRead());
        }/*from ww w.  j  a v  a  2  s.  c  o m*/

        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;

        try {
            FileInputStream inStream = new FileInputStream(sourceFile);
            source = inStream.getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }
}

Related

  1. copyFile(File source, File target)
  2. copyFile(File sourceFile, File destFile)
  3. copyFile(File sourceFile, File destFile)
  4. copyFile(File sourceFile, File destFile)
  5. copyFile(File sourceFile, File destFile)
  6. copyFile(File sourceFile, File destFile)
  7. copyFile(File sourceFile, File destFile)
  8. copyFile(File sourceFile, File destFile)
  9. copyFile(File sourceFile, File destFile)