Java Copy File copyFilesBinaryRecursively(File fromLocation, File toLocation, FileFilter fileFilter)

Here you can find the source of copyFilesBinaryRecursively(File fromLocation, File toLocation, FileFilter fileFilter)

Description

Recursively copies files and subdirectories from fromLocation to toLocation using FileFilter fileFliter

License

Open Source License

Parameter

Parameter Description
fromLocation a parameter
toLocation a parameter
fileFilter a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFilesBinaryRecursively(File fromLocation, File toLocation, FileFilter fileFilter)
        throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007-2009 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is 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
 *
 * Contributor://w  ww  . j  av  a  2 s.c o m
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;

public class Main {
    /**
     * Recursively copies files and subdirectories from fromLocation to
     * toLocation using FileFilter fileFliter
     * 
     * @param fromLocation
     * @param toLocation
     * @param fileFilter
     * @throws IOException
     */
    public static void copyFilesBinaryRecursively(File fromLocation, File toLocation, FileFilter fileFilter)
            throws IOException {
        if (fromLocation.exists()) {
            for (File fileToCopy : fromLocation.listFiles(fileFilter)) {
                if (fileToCopy.isDirectory()) {
                    File newToDir = new File(toLocation, fileToCopy.getName());
                    newToDir.mkdir();
                    copyFilesBinaryRecursively(fileToCopy, newToDir, fileFilter);
                } else {
                    copyFilesBinary(fileToCopy, toLocation);
                }
            }
        }
    }

    /**
     * Copies binary file originalFile to location toLocation
     * 
     * @param originalFile
     * @param toLocation
     * @throws IOException
     */
    public static void copyFilesBinary(File originalFile, File toLocation) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(originalFile);
            fos = new FileOutputStream(new File(toLocation, originalFile.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead); // write
            }

        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
        }
    }
}

Related

  1. copyFiles(String strPath, String dstPath, String srcExtension)
  2. copyFiles(String[] sourceFiles, String toDir, boolean overwrite)
  3. copyFileSafe(File destinationFileName, File sourceFileName)
  4. copyFileSafe(File in, File out)
  5. copyFilesBinary(String srcFile, String destDir)
  6. copyFilesFromDirToDir(String originalDir, String targetDir)
  7. copyFilesRecursive(File src, File dest)
  8. copyFilesToLocal(String destination, String source)