Java File Copy nio copy(File src, File dest)

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

Description

copy

License

Open Source License

Declaration

public static void copy(File src, File dest) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 Red Hat, Inc.//from  ww w.j  av a2  s. co  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Red Hat Incorporated - initial API and implementation
 *******************************************************************************/

import java.io.File;
import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class Main {
    public static void copy(File src, File dest) throws IOException {

        if (src.isDirectory()) {

            // ensure destination directory exists
            if (!dest.exists()) {
                dest.mkdir();
            }

            // copy all children files recursively
            String files[] = src.list();

            for (String file : files) {
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                copy(srcFile, destFile);
            }

        } else { // is a file, manually copy the contents over
            Files.copy(src.toPath(), dest.toPath(),
                    StandardCopyOption.REPLACE_EXISTING);
        }
    }
}

Related

  1. copy(File sourceFile, File destFile)
  2. copy(File sourceFile, File destFile)
  3. copy(File sourceFile, File destinationFile)
  4. copy(File sourceFile, File targetFile)
  5. copy(File src, File dest)
  6. copy(File src, File dest)
  7. copy(File src, File dest)
  8. copy(File src, File dist)
  9. copy(File src, File dst)