Java FileChannel Copy copyFile(File src, File dest)

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

Description

copy File

License

Apache License

Declaration

public static boolean copyFile(File src, File dest) 

Method Source Code

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    public static boolean copyFile(File src, File dest) {
        boolean result = false;
        try {/*from   w  w w.j av a2s  .  com*/
            FileInputStream fis = new FileInputStream(src);
            FileChannel sourcefc = fis.getChannel();
            FileOutputStream fos = new FileOutputStream(dest);
            FileChannel targetfc = fos.getChannel();
            if (sourcefc.transferTo(0, sourcefc.size(), targetfc) == sourcefc.size()) {
                result = true;
            }
            targetfc.close();
            sourcefc.close();
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

Related

  1. copyFile(File sourceFile, File destinationFile)
  2. copyFile(File sourceFile, File destinationFile)
  3. copyFile(File sourceFile, File targetFile)
  4. copyFile(File sourceFile, File targetFile)
  5. copyFile(File sourceFile, File targetFile)
  6. copyFile(File src, File dest)
  7. copyFile(File src, File dest)
  8. copyFile(File src, File dest)
  9. copyFile(File src, File destination)