Java Recursive Copy recursiveCopy(File src, File dest)

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

Description

recursive Copy

License

Open Source License

Declaration

private static void recursiveCopy(File src, File dest) throws IOException 

Method Source Code

//package com.java2s;
/**//from   www  .  j  a v  a2 s.  c o m
 * Copyright (C) 2010-2016 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3.0 of the License, or
 * (at your option) any later version.
 *
 * EvoSuite 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
 * Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with EvoSuite. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

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

        if (src.isDirectory()) {

            //the destination might not exist. if so, let's create it
            if (!dest.exists()) {
                dest.mkdirs();
            }

            //iterate over the children
            for (String file : src.list()) {
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                //recursive call
                recursiveCopy(srcFile, destFile);
            }

        } else {

            boolean sameTime = src.lastModified() == dest.lastModified();
            if (sameTime) {
                //file was not modified, so no need to copy over
                return;
            }

            try (InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest);) {

                byte[] buffer = new byte[2048];

                int length;
                //copy the file content in bytes
                while ((length = in.read(buffer)) > 0) {
                    out.write(buffer, 0, length);
                }
            }

            //as it is a copy, make sure to get same time stamp as src, otherwise it ll be always copied over
            dest.setLastModified(src.lastModified());
        }

    }
}

Related

  1. recursiveCopy(File copyFromDir, File copyToDir, boolean overwrite)
  2. recursiveCopy(File sourceFile, File targetLocation)
  3. recursiveCopyDir(File source, File destination)
  4. recursiveCopyFile(File srcDir, File destDir)