Java FileChannel Copy copyFile(File source, File target)

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

Description

copy File

License

Apache License

Declaration

public static void copyFile(File source, File target) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.nio.channels.FileChannel;

public class Main {

    public static void copyFile(File source, File target) {
        InputStream is = null;/*from   w  w w. ja v  a  2s.c o m*/
        try {
            is = new FileInputStream(source);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        copyFile((FileInputStream) is, target);
    }

    public static void copyFile(FileInputStream source, File target) {
        FileChannel in = null;
        FileChannel out = null;
        FileInputStream inStream = null;
        FileOutputStream outStream = null;
        try {
            inStream = source;
            outStream = new FileOutputStream(target);
            in = inStream.getChannel();
            out = outStream.getChannel();
            in.transferTo(0, in.size(), out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(inStream);
            close(in);
            close(outStream);
            close(out);
        }
    }

    public static void close(Closeable closeable) {
        try {
            closeable.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. copyFile(File source, File destination, boolean overwrite)
  2. copyFile(File source, File destination, long chunkSize, boolean overwrite)
  3. copyFile(File source, File target)
  4. copyFile(File source, File target)
  5. copyFile(File source, File target)
  6. copyFile(File source, File target)
  7. copyFile(File sourceFile, File destFile)
  8. copyFile(File sourceFile, File destFile)
  9. copyFile(File sourceFile, File destFile)