Java FileInputStream Copy copyFile(File src, File dest)

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

Description

copy File

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/**/*from w w  w. j ava  2 s.co 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.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static final int DEFAULT_BUFFER_SIZE = 4096;

    public static void copyFile(File src, File dest) throws IOException {
        copyFile(src, dest, null);
    }

    public static void copyFile(File src, File dest, FilenameFilter filter) throws IOException {
        if (src.getCanonicalPath().equals(dest.getCanonicalPath()) == false) {
            if (src.isDirectory()) {

                mkdirs(dest);
                List<File> list = getFiles(src, filter);
                for (File f : list) {
                    if (f.isFile()) {
                        File target = new File(getCopyParent(src, dest, f), f.getName());
                        copySingleFile(f, target);
                    }
                }

            } else if (dest.isDirectory()) {
                mkdirs(dest);
                File target = new File(dest, src.getName());
                copySingleFile(src, target);
            } else {
                copySingleFile(src, dest);
            }
        }
    }

    public static void mkdirs(File dir) throws IOException {
        if (dir.exists()) {
            if (!dir.isDirectory()) {
                throw new IOException(
                        "Failed to create directory '" + dir + "', regular file already existed with that name");
            }

        } else {
            if (!dir.mkdirs()) {
                throw new IOException("Failed to create directory '" + dir + "'");
            }
        }
    }

    static List<File> getFiles(File dir, FilenameFilter filter) {
        List<File> result = new ArrayList<File>();
        getFiles(dir, result, filter);
        return result;
    }

    static void getFiles(File dir, List<File> list, FilenameFilter filter) {
        if (!list.contains(dir)) {
            list.add(dir);
            String[] fileNames = dir.list(filter);
            for (int i = 0; i < fileNames.length; i++) {
                File f = new File(dir, fileNames[i]);
                if (f.isFile()) {
                    list.add(f);
                } else {
                    getFiles(dir, list, filter);
                }
            }
        }
    }

    static File getCopyParent(File from, File to, File src) {
        File result = null;
        File parent = src.getParentFile();
        String fromPath = from.getAbsolutePath();
        if (parent.getAbsolutePath().equals(fromPath)) {
            // one level down
            result = to;
        } else {
            String parentPath = parent.getAbsolutePath();
            String path = parentPath.substring(fromPath.length());
            result = new File(to.getAbsolutePath() + File.separator + path);
        }
        return result;
    }

    public static void copySingleFile(File src, File dest) throws IOException {
        FileInputStream fileSrc = new FileInputStream(src);
        FileOutputStream fileDest = new FileOutputStream(dest);
        copyInputStream(fileSrc, fileDest);
    }

    public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
        try {
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int len = in.read(buffer);
            while (len >= 0) {
                out.write(buffer, 0, len);
                len = in.read(buffer);
            }
        } finally {
            in.close();
            out.close();
        }
    }
}

Related

  1. copyFile(File src, File dest)
  2. copyFile(File src, File dest)
  3. copyFile(File src, File dest)
  4. copyFile(File src, File dest)
  5. copyFile(File src, File dest)
  6. copyFile(File src, File dest)
  7. copyFile(File src, File dest, int bufSize)
  8. copyFile(File src, File dst)
  9. copyFile(File src, File dst)