Java FileChannel Copy copyFile(File src, File target)

Here you can find the source of copyFile(File src, File target)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File src, File target) 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 {
    public static void copyFile(File src, File target) throws IOException {
        prepareWrite(target);//from   w  w w  .  ja va2  s .co m
        try (FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(target)) {
            FileChannel chin = in.getChannel();
            FileChannel chout = out.getChannel();
            chin.transferTo(0, chin.size(), chout);
        }
    }

    public static void prepareWrite(File file) throws IOException {
        File parent = file.getParentFile();
        if (parent != null && !parent.exists()) {
            mkdirs(parent);
        }
    }

    public static void mkdirs(File dir) throws IOException {
        if (!dir.mkdirs()) {
            throw new IOException("Cannot mkdirs: " + dir);
        }
    }
}

Related

  1. copyFile(File src, File dst)
  2. copyFile(File src, File dst)
  3. copyFile(File src, File dst)
  4. copyFile(File src, File dst)
  5. copyFile(File src, File dst)
  6. copyFile(File src, File target)
  7. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  8. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  9. copyFile(File srcFile, File destFile, boolean preserveFileDate)