<span style="white-space:pre"> </span>/***
* 将源目录下的文件拷贝到另一个目录下
*
* @param originDirectory 源目录路径
* @param targetDirectory 目标目录路径
*/
public static void copyAllFile(String originDirectory,String targetDirectory) {
// 源路径File实例
File origindirectory = new File(originDirectory);
// 目标路径File实例
File targetdirectory = new File(targetDirectory);
if (!targetdirectory.exists()) {
// 目录不存在的情况下,创建目录。
targetdirectory.mkdirs();
}
// 判断是不是正确的路径
if (!origindirectory.isDirectory() || !targetdirectory.isDirectory()) {
log.error("不是正确的目录!");
return;
}
// 源目录中的所有文件
File[] fileList = origindirectory.listFiles();
// 循环源目录中的文件拷贝到目标目录下
for (File file : fileList) {
// 判断是不是文件
if (!file.isFile()) {
continue;
}
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(file);
output = new FileOutputStream(new File(targetdirectory,file.getName()));
byte[] buf = new byte[1024000];
int count = 0;
while ((count = input.read(buf)) != -1) {
output.write(buf, 0, count);
output.flush();
}
output.flush();
} catch (Exception e) {
e.printStackTrace();
log.error(e, e);
} finally {
if (null != input) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
log.error(e, e);
}
}
if (null != output) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
log.error(e, e);
}
}
}
}
}