Here you can find the source of copyFiles(String[] sourceFiles, String toDir, boolean overwrite)
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. |
Parameter | Description |
---|---|
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
//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"); } }