Java FileChannel Copy copyFile(File from, File to)

Here you can find the source of copyFile(File from, File to)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File from, File to) throws IOException 

Method Source Code

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

import java.io.*;

import java.nio.channels.FileChannel;

public class Main {
    public static void copyFile(File from, File to) throws IOException {
        if (!from.exists()) {
            throw new FileNotFoundException();
        }//w  ww . j  a  v  a  2 s. c o m
        if (from.isDirectory() || to.isDirectory()) {
            throw new FileNotFoundException();
        }
        FileInputStream fi = null;
        FileChannel in = null;
        FileOutputStream fo = null;
        FileChannel out = null;
        try {
            if (!to.exists()) {
                to.createNewFile();
            }
            fi = new FileInputStream(from);
            in = fi.getChannel();
            fo = new FileOutputStream(to);
            out = fo.getChannel();
            in.transferTo(0, in.size(), out);
        } finally {
            if (fi != null)
                fi.close();
            if (in != null)
                in.close();
            if (fo != null)
                fo.close();
            if (out != null)
                out.close();
        }
    }
}

Related

  1. copyFile(File file, String destDir)
  2. copyFile(File fileIn, File fileOut)
  3. copyFile(File from, File to)
  4. copyFile(File from, File to)
  5. copyFile(File from, File to)
  6. copyFile(File from, File to, long fromoffset, long tooffset, long size)
  7. copyFile(File in, File out)
  8. copyFile(File in, File out)
  9. copyFile(File in, File out)