Java FileChannel Copy copyFile(File aFromFile, String aToFilename)

Here you can find the source of copyFile(File aFromFile, String aToFilename)

Description

copy File

License

Apache License

Declaration

public static void copyFile(File aFromFile, String aToFilename) 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 aFromFile, String aToFilename) throws IOException {
        File outdir = getFileDirOnly(new File(aToFilename));
        outdir.mkdirs();//from w  w w . j  a  v  a  2s.  c om

        FileInputStream fis = new FileInputStream(aFromFile.getAbsolutePath());
        FileChannel inChannel = fis.getChannel();
        FileOutputStream fos = new FileOutputStream(aToFilename);
        FileChannel outChannel = fos.getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (IOException e) {
            throw e;
        } finally {
            if (fis != null)
                fis.close();
            if (fos != null)
                fos.close();
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    }

    public static File getFileDirOnly(File aFile) {
        if (aFile != null) {
            String s1 = aFile.getPath();
            String s2 = aFile.getName();
            return new File(s1.substring(0, s1.length() - s2.length()));
        } else {
            return null;
        }
    }
}

Related

  1. copy2(String sourceFileName, String destFileName)
  2. copyAFile(File source, File target)
  3. copyAll(File source, File targetDir)
  4. copyAndReplace(File from, File to)
  5. copyChannel(ReadableByteChannel in, long length, FileChannel out)
  6. copyFile(File copyFrom, File copyTo)
  7. copyFile(File file, String destDir)
  8. copyFile(File fileIn, File fileOut)
  9. copyFile(File from, File to)