Java FileInputStream Copy copyFile(File fileToCopy, File targetDir)

Here you can find the source of copyFile(File fileToCopy, File targetDir)

Description

Copies specified file to the target directory

License

Open Source License

Parameter

Parameter Description
fileToCopy - copied file
targetDir - the directory, which the file will be copied to

Declaration

public static void copyFile(File fileToCopy, File targetDir) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008, 2009 SOPERA GmbH.
 * All rights reserved. This program and the accompanying materials
 * are 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
 *
 * Contributors://from  w ww .j av  a  2 s.c o m
 *     SOPERA GmbH - initial API and implementation
 *******************************************************************************/

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

import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Copies specified file to the target directory
     * @param fileToCopy  - copied file
     * @param targetDir - the directory, which the file will be copied to
     */
    public static void copyFile(File fileToCopy, File targetDir) {
        if (!fileToCopy.exists()) {
            throw new IllegalArgumentException();
        }
        if (!targetDir.isDirectory()) {
            throw new IllegalArgumentException();
        }
        copyDirectory(fileToCopy, new File(targetDir, fileToCopy.getName()), null);
    }

    /**
     * Copies the directory from the source location to the target one
     * if fileExtensions is null all subsequent files will be copied 
     * @param sourceLocation - source directory
     * @param targetLocation - target directory
     * @param fileExtensions the list of the file name filters, that specify which files to copy  
     */
    public static void copyDirectory(File sourceLocation, File targetLocation, String[] fileExtensions) {
        try {
            if (sourceLocation.isDirectory()) {
                if (!targetLocation.exists()) {
                    org.eclipse.core.runtime.Assert.isTrue(targetLocation.mkdir());
                }

                String[] children = sourceLocation.list();
                for (String child : children) {

                    boolean eligibleByExtension = false;
                    if (fileExtensions != null) {
                        for (String fileExtension : fileExtensions) {
                            if (child.toLowerCase().endsWith(fileExtension.toLowerCase())) {
                                eligibleByExtension = true;
                                break;
                            }
                        }
                    } else {
                        eligibleByExtension = true;
                    }

                    if (eligibleByExtension) {
                        copyDirectory(new File(sourceLocation, child), new File(targetLocation, child),
                                fileExtensions);
                    }
                }
            } else {
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = new FileInputStream(sourceLocation);
                    out = new FileOutputStream(targetLocation);

                    // Copy the bits from instream to outstream
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                } finally {
                    if (in != null) {
                        in.close();
                    }
                    if (out != null) {
                        out.close();
                    }
                }
            }
        } catch (Exception ex) {
            throw new IllegalStateException(ex.getMessage(), ex);
        }
    }
}

Related

  1. copyFile(File file, File destPath)
  2. copyFile(File file, File dir)
  3. copyFile(File file, File newFile, boolean overwrite, boolean setLastModified)
  4. copyFile(File file, OutputStream os)
  5. copyFile(File fileSrc, File fileDest)
  6. copyFile(File from, File to)
  7. copyFile(File from, File to)
  8. copyFile(File from, File to)
  9. copyFile(File from, File to)