Java Folder Copy nio copyFolders(File sourceFolder, File destFolder)

Here you can find the source of copyFolders(File sourceFolder, File destFolder)

Description

copy Folders

License

Open Source License

Declaration

public static void copyFolders(File sourceFolder, File destFolder) 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 copyFolders(File sourceFolder, File destFolder) throws IOException {
        if (!destFolder.exists())
            destFolder.mkdirs();/*from  ww  w  . j  a  va 2 s. com*/
        File[] listFiles = sourceFolder.listFiles();
        if (listFiles != null) {
            for (File srcFile : listFiles) {
                if (srcFile.isDirectory())
                    copyFolders(srcFile, new File(destFolder, srcFile.getName()));
                if (srcFile.isFile()) {
                    File destFile = new File(destFolder, srcFile.getName());
                    destFile.createNewFile();
                    copyFiles(srcFile, destFile);
                }
            }
        }
    }

    public static void copyFiles(File source, File dest) throws IOException {
        FileChannel fichannel = new FileInputStream(source).getChannel();
        FileChannel foChannel = new FileOutputStream(dest).getChannel();

        long size = fichannel.size();
        fichannel.transferTo(0, size, foChannel);
    }
}

Related

  1. copyFolder(File src, File dest)
  2. copyFolder(File src, File dst, FilenameFilter filter)
  3. copyFolder(final File from, final File to)
  4. copyFolder(String path, String resultPath)
  5. copyFolderNio(String oldpathall, String newpath)