Java FileInputStream Copy copyFileToDir(String targetDir, String[] filePath)

Here you can find the source of copyFileToDir(String targetDir, String[] filePath)

Description

Copy many files to one directory

License

Apache License

Parameter

Parameter Description
targetDir a parameter
filePath a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFileToDir(String targetDir, String[] filePath) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w  ww .  j a v  a 2 s. c  o  m*/
 *  Copyright 2009, 2011 Welocalize, Inc. 
 *  
 *  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.
 *  
 */

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

public class Main {
    /**
     * Copy many files to one directory
     * 
     * @param targetDir
     * @param filePath
     * @throws IOException
     */
    public static void copyFileToDir(String targetDir, String[] filePath) throws IOException {
        File targetFile = new File(targetDir);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        } else {
            if (!targetFile.isDirectory()) {
                return;
            }
        }
        for (String path : filePath) {
            File file = new File(path);
            if (file.isDirectory()) {
                copyFileToDir(targetDir + File.separator + file.getName(), listFile(file));
            } else {
                copyFileToDir(targetDir, file);
            }
        }
    }

    /**
     * Copy one file to a directory
     * 
     * @param targetFile
     * @param file
     * @param newName
     * @throws IOException
     */
    public static void copyFileToDir(String targetDir, File file) throws IOException {
        String newFile = targetDir + File.separator + file.getName();
        File tFile = new File(newFile);
        copyFile(tFile, file);
    }

    /**
     * List all the files path
     * 
     * @param dir
     * @return
     */
    public static String[] listFile(File dir) {
        String absolutPath = dir.getAbsolutePath();
        String[] paths = dir.list();
        String[] files = new String[paths.length];
        for (int i = 0; i < paths.length; i++) {
            files[i] = absolutPath + File.separator + paths[i];
        }
        return files;
    }

    /**
     * Copy file to file
     * 
     * @param targetFile
     * @param file
     * @throws IOException
     */
    public static void copyFile(File targetFile, File file) throws IOException {

        InputStream is = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(targetFile);
        byte[] buffer = new byte[1024];
        int i = 0;
        while ((i = is.read(buffer)) != -1) {
            fos.write(buffer, 0, i);
        }
        is.close();
        fos.close();
    }

    /**
     * Copy file(InputStream) to file
     * 
     * @param targetFile
     * @param file
     * @throws IOException
     */
    public static void copyFile(File targetFile, InputStream file) throws IOException {

        FileOutputStream fos = new FileOutputStream(targetFile);
        byte[] buffer = new byte[1024];
        int i = 0;
        while ((i = file.read(buffer)) != -1) {
            fos.write(buffer, 0, i);
        }
        file.close();
        fos.close();
    }
}

Related

  1. copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile)
  2. copyFileToContainer(File pChild, IContainer pContainer)
  3. copyFileToDestDir(String srcFilePath, String destFileDir)
  4. copyFileToDir(File oldFile, File outputDir)
  5. copyFileToDir(File sourceFile, File destDir)
  6. CopyFileToDirectory(String file_name, String from_directory, String to_directory)
  7. copyFileToFile(File file, File destFile, boolean overwrite)
  8. copyFileToFile(File input, String outputPath, String exportName)
  9. copyFileToOutputStream(File file, OutputStream stream)