Here you can find the source of copyFileToDir(String targetDir, String[] filePath)
Parameter | Description |
---|---|
targetDir | a parameter |
filePath | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyFileToDir(String targetDir, String[] filePath) throws IOException
//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(); } }