Java File Copy nio copy(File base_path, File in, File out)

Here you can find the source of copy(File base_path, File in, File out)

Description

copy

License

Open Source License

Declaration

public static void copy(File base_path, File in, File out) throws Exception 

Method Source Code

//package com.java2s;
/**/*from   w w  w  .jav a  2s.c o  m*/
 * ##################
 * # Tilde Launcher #
 * ##################
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author Riccardo B.
 * @version 0.1
 */

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import java.io.File;

import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void copy(File in, File out) throws Exception {
        copy(in, in, out);
    }

    public static void copy(File base_path, File in, File out) throws Exception {

        File[] l = in.listFiles();
        for (File f : l) {
            if (f.isDirectory()) {
                copy(base_path, f, out);
            } else {
                String relative_out = f.getAbsolutePath().replace(base_path.getAbsolutePath(), "");

                File dest_f = new File(out.getAbsolutePath() + relative_out);
                dest_f.mkdirs();
                Path source = f.toPath();
                Path dest = dest_f.toPath();

                System.out.println("Copying " + source + " to " + dest);
                Files.copy(source, dest, REPLACE_EXISTING);

            }
        }
    }

    public static void mkdirs(String f) {
        new File(f).mkdirs();
    }
}

Related

  1. copy(File a, File b)
  2. copy(File copied, File destination)
  3. copy(File file, File toDirectory)
  4. copy(File from, File to)
  5. copy(File from, File to)