Java Copy File copyFiles(String[] sourceFiles, String toDir, boolean overwrite)

Here you can find the source of copyFiles(String[] sourceFiles, String toDir, boolean overwrite)

Description

Copy a list of files to a specifc folder.

License

Open Source License

Parameter

Parameter Description
sourceFiles An array containing the name of the files to be copied.
toDir The destination forlder.
overwrite Indicates if the existing files in the destination folder will be overwritten.

Exception

Parameter Description
IOException If there is a file in the destination folder with same source name.

Return

The new absolute file names.

Declaration

public static String[] copyFiles(String[] sourceFiles, String toDir, boolean overwrite) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;

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

public class Main {
    /**//from  www. j a  va2  s  . c  o  m
     * Copy a list of files to a specifc folder.
     * 
     * @param sourceFiles An array containing the name of the files to be copied.
     * @param toDir The destination forlder.
     * @param overwrite Indicates if the existing files in the destination folder will be
     * overwritten.
     * @return The new absolute file names.
     * @throws IOException If there is a file in the destination folder with same source name.
     */
    public static String[] copyFiles(String[] sourceFiles, String toDir, boolean overwrite) throws IOException {
        String[] newFileNames = new String[sourceFiles.length];
        for (int i = 0; i < sourceFiles.length; i++) {
            File inFile = new File(sourceFiles[i]);
            File outFile = new File(toDir + getSeparator() + inFile.getName());
            boolean fileExists = false;
            if (outFile.exists()) {
                fileExists = true;
                if (overwrite) {
                    if (!outFile.delete()) {
                        throw new IOException(outFile.getAbsolutePath() + " cannot be overwritten.");
                    }
                    fileExists = false;
                }
            }

            if (!fileExists) {
                byte[] b = new byte[(int) inFile.length()];
                InputStream in = new FileInputStream(inFile);
                OutputStream out = new FileOutputStream(outFile);
                in.read(b);
                out.write(b);
                out.close();
                in.close();
                newFileNames[i] = outFile.getAbsolutePath();
            }
        }
        return newFileNames; // return the new locations
    }

    /**
     * Returns the system separator.
     * 
     * @return The system separator string.
     */
    public static String getSeparator() {
        return System.getProperty("file.separator");
    }
}

Related

  1. copyFiles(String srcFilePath, String destFilePath)
  2. copyFiles(String srcFolderStr, String destFolderStr)
  3. copyFiles(String srcPath, String destPath)
  4. CopyFiles(String strOrgFile, String strDestFile, boolean bFailIfExists)
  5. copyFiles(String strPath, String dstPath, String srcExtension)
  6. copyFileSafe(File destinationFileName, File sourceFileName)
  7. copyFileSafe(File in, File out)
  8. copyFilesBinary(String srcFile, String destDir)
  9. copyFilesBinaryRecursively(File fromLocation, File toLocation, FileFilter fileFilter)