Java FileChannel Copy copy(final String aSrcPath, final String aDestPath)

Here you can find the source of copy(final String aSrcPath, final String aDestPath)

Description

copy

License

Apache License

Declaration

public static void copy(final String aSrcPath, final String aDestPath) 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 copy(final String aSrcPath, final String aDestPath) throws IOException {
        copy(new File(aSrcPath), new File(aDestPath));
    }/*from  w  w w .ja  va2  s  .co m*/

    public static void copy(final File aSrcFile, final File aDestFile) throws IOException {
        if (aSrcFile.isDirectory()) {
            aDestFile.mkdirs();
            File[] files = aSrcFile.listFiles();
            for (File file : files) {
                String srcFile = aSrcFile.getAbsolutePath() + "\\" + file.getName();
                String destFile = aDestFile.getAbsolutePath() + "\\" + file.getName();
                copy(new File(srcFile), new File(destFile));
            }
        } else if (aSrcFile.isFile()) {
            FileChannel srcChannel = null;
            FileChannel destChannel = null;
            try {
                srcChannel = new FileInputStream(aSrcFile).getChannel();
                destChannel = new FileOutputStream(aDestFile).getChannel();
                srcChannel.transferTo(0, srcChannel.size(), destChannel);
            } finally {
                if (null != srcChannel) {
                    srcChannel.close();
                }
                if (null != destChannel) {
                    destChannel.close();
                }
            }
        }
    }
}

Related

  1. copy(FileInputStream iStream, FileOutputStream oStream)
  2. copy(final File aCopyFrom, final File aCopyTo)
  3. copy(final File fromFile, final File toFile)
  4. copy(final File source, final File dest)
  5. copy(final File src, File dst, final boolean overwrite)
  6. copy(final String aSrcPath, final String aDestPath)
  7. copy(String fromPath, String toPath)
  8. copy(String source, String destination)
  9. copy(String source, String destination, boolean recursive)