Android Directory Copy copyDirectory(String sourceDirName, String destinationDirName)

Here you can find the source of copyDirectory(String sourceDirName, String destinationDirName)

Description

copy Directory

License

Apache License

Declaration

public static void copyDirectory(String sourceDirName,
            String destinationDirName) 

Method Source Code

/*//w ww  .  jav  a 2s  . c  o m
 * Copyright 2004-2005 Germinus XXI, Liferay LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This is a derived work from source code taken from Liferay.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import org.apache.commons.lang.StringUtils;

public class Main{
    public static void copyDirectory(String sourceDirName,
            String destinationDirName) {

        copyDirectory(new File(sourceDirName), new File(destinationDirName));
    }
    public static void copyDirectory(File source, File destination) {
        if (source.exists() && source.isDirectory()) {
            if (!destination.exists()) {
                destination.mkdirs();
            }

            File[] fileArray = source.listFiles();

            for (int i = 0; i < fileArray.length; i++) {
                if (fileArray[i].isDirectory()) {
                    copyDirectory(fileArray[i],
                            new File(destination.getPath() + File.separator
                                    + fileArray[i].getName()));
                } else {
                    copyFile(fileArray[i], new File(destination.getPath()
                            + File.separator + fileArray[i].getName()));
                }
            }
        }
    }
    public static boolean exists(String fileName) {
        File file = new File(fileName);

        return file.exists();
    }
    public static void mkdirs(String pathName) {
        File file = new File(pathName);
        file.mkdirs();
    }
    public static String[] listFiles(String fileName) throws IOException {
        return listFiles(new File(fileName));
    }
    public static String[] listFiles(File file) throws IOException {
        List files = new ArrayList();

        File[] fileArray = file.listFiles();

        for (int i = 0; i < fileArray.length; i++) {
            if (fileArray[i].isFile()) {
                files.add(fileArray[i].getName());
            }
        }

        return (String[]) files.toArray(new String[0]);
    }
    public static String getPath(String fullFileName) {
        int pos = fullFileName.lastIndexOf("/");

        if (pos == -1) {
            pos = fullFileName.lastIndexOf("\\");
        }

        String shortFileName = fullFileName.substring(0, pos);

        if (StringUtils.isEmpty(shortFileName)) {
            return "/";
        }

        return shortFileName;
    }
    public static void copyFile(String sourceFileName,
            String destinationFileName) {

        copyFile(new File(sourceFileName), new File(destinationFileName));
    }
    public static void copyFile(File source, File destination) {
        if (!source.exists()) {
            return;
        }

        if ((destination.getParentFile() != null)
                && (!destination.getParentFile().exists())) {

            destination.getParentFile().mkdirs();
        }

        try {
            FileChannel srcChannel = new FileInputStream(source)
                    .getChannel();
            FileChannel dstChannel = new FileOutputStream(destination)
                    .getChannel();

            dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

            srcChannel.close();
            dstChannel.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

Related

  1. copyDirectory(@NotNull String srcDir, @NotNull String dstDir, @NotNull final List excludes)
  2. copyDirectory(File from, File to)
  3. copyDirectory(File from, File to, byte[] buffer)
  4. copyDirectory(File source, File destination)
  5. copyDirectory(String from, String to)
  6. copyDirectory(File sourceDir, File targetDir)