Java FileChannel Copy copy(final String aSrcPath, final String aDestPath)

Here you can find the source of copy(final String aSrcPath, final String aDestPath)

Description

copy

License

Apache License

Declaration

public static void copy(final String aSrcPath, final String aDestPath) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  www.j  ava  2s.c  o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.util.ArrayList;

import java.util.List;

import java.util.regex.Pattern;

public class Main {

    public static void copy(final String aSrcPath, final String aDestPath) throws IOException {
        copy(new File(aSrcPath), new File(aDestPath));
    }

    public static void copy(final File aSrcFile, final File aDestFile) throws IOException {
        if (aSrcFile.isDirectory()) {
            aDestFile.mkdirs();
            File[] files = aSrcFile.listFiles();
            for (File file : files) {
                File srcFile = Paths.get(aSrcFile.getAbsolutePath(), file.getName()).toFile();
                File destFile = Paths.get(aDestFile.getAbsolutePath(), file.getName()).toFile();
                copy(srcFile, destFile);
            }
        } else if (aSrcFile.isFile()) {
            FileChannel srcChannel = null;
            FileChannel destChannel = null;
            try {
                srcChannel = new FileInputStream(aSrcFile).getChannel();
                destChannel = new FileOutputStream(aDestFile).getChannel();
                srcChannel.transferTo(0, srcChannel.size(), destChannel);
            } finally {
                if (null != srcChannel) {
                    srcChannel.close();
                }
                if (null != destChannel) {
                    destChannel.close();
                }
            }
        }
    }

    public static final List<File> listFiles(final String aDirectory) {
        return listFiles(aDirectory, (Pattern) null);
    }

    public static final List<File> listFiles(final File aDirectory) {
        return listFiles(aDirectory, (Pattern) null);
    }

    public static final List<File> listFiles(final String aDirectory, final String aPattern) {
        return listFiles(aDirectory, Pattern.compile(aPattern));
    }

    public static final List<File> listFiles(final File aDirectory, final String aPattern) {
        return listFiles(aDirectory, Pattern.compile(aPattern));
    }

    public static final List<File> listFiles(final String aDirectory, final Pattern aPattern) {
        return listFiles(new File(aDirectory), aPattern);
    }

    public static final List<File> listFiles(final File aDirectory, final Pattern aPattern) {
        List<File> files = new ArrayList<File>();
        readDir(aDirectory, aPattern, files);
        return files;
    }

    private static void readDir(final File aDir, final Pattern aPattern, final List<File> aFiles) {
        File[] files = aDir.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            if (!file.exists()) {
                continue;
            } else if (file.isDirectory()) {
                readDir(file, aPattern, aFiles);
            } else if (file.isFile())
                readFile(file, aPattern, aFiles);
        }
    }

    private static void readFile(final File aFile, final Pattern aPattern, final List<File> aFiles) {
        String path = aFile.getAbsolutePath();
        if (null == aPattern || aPattern.matcher(path).matches()) {
            aFiles.add(aFile);
        }
    }
}

Related

  1. copy(final File aCopyFrom, final File aCopyTo)
  2. copy(final File fromFile, final File toFile)
  3. copy(final File source, final File dest)
  4. copy(final File src, File dst, final boolean overwrite)
  5. copy(final String aSrcPath, final String aDestPath)
  6. copy(String fromPath, String toPath)
  7. copy(String source, String destination)
  8. copy(String source, String destination, boolean recursive)
  9. copy(String sourceFile, String targetFile)