Java実装フォルダcopyとdelete
仕事のためjavaでフォルダのcopyとdeleteを実現し、後で検索するためにブログに置きます.
package com.founder.assets.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.fileupload.util.Streams;
public class FolderUtil {
/**
*
*@param sourceDir
*@param targetDir
*/
public static void copyFolder(String sourceDir, String targetDir) throws IOException {
// ,
if (!((new File(targetDir)).exists())) {
(new File(targetDir)).mkdirs();
}
//
File[] file = (new File(sourceDir)).listFiles();
if(file == null){
return;
}
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
//
File sourceFile = file[i];
//
File targetFile = new File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
copyFile(sourceFile, targetFile);
}
}
}
/**
*
*@param sourceFile
*@param targetFile
*/
public static void copyFile(File sourceFile, File targetFile) throws IOException {
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
//
inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
//
outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
Streams.copy(inBuff, outBuff, false);
outBuff.flush();
} finally {
//
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
}
public static void copyFile(String xmlFile, String destName) throws IOException {
copyFile(new File(xmlFile), new File(destName));
}
/**
* ,
*@param deletePath
*@return true, false。
*/
public static boolean DeleteFolder(String deletePath) {
boolean flag = false;
File file = new File(deletePath);
//
if (!file.exists()) { // false
return flag;
} else {
//
if (file.isFile()) { //
return deleteFile(deletePath);
} else { //
return deleteDirectory(deletePath);
}
}
}
/**
*
* @param deletePath
* @return true, false
*/
public static boolean deleteFile(String deletePath) {
boolean flag = false;
File file = new File(deletePath);
//
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* ( )
* @param sPath
* @return true, false
*/
public static boolean deleteDirectory(String deletePath) {
// deletePath ,
if (!deletePath.endsWith(File.separator)) {
deletePath = deletePath + File.separator;
}
File dirFile = new File(deletePath);
// dir , ,
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
// ( )
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
//
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag) break;
} //
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag) break;
}
}
if (!flag) return false;
//
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
/**
* ,
* @param deletePath
* @return
* @return true, false
*/
public static boolean cleanFolder(String cleanPath) {
// cleanPath ,
if (!cleanPath.endsWith(File.separator)) {
cleanPath = cleanPath + File.separator;
}
File dirFile = new File(cleanPath);
// dir , ,
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
// ( )
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
//
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag) break;
} //
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag) break;
}
}
return flag;
}
public static void main(String agrs[]) throws IOException {
//String sourceDir = "D:\\foldercopyanddel\\sourcedir";
//String targetDir = "D:\\foldercopyanddel\\targetdir";
//copyFolder(sourceDir, targetDir);
//System.out.print("copyFolder end!");
//String targetDir = "D:\\foldercopyanddel\\targetdir";
//DeleteFolder(targetDir);
//System.out.print("deleteFolder end!");
//String targetDir = "D:\\foldercopyanddel\\targetdir";
//cleanFolder(targetDir);
//System.out.print("cleanFolder end!");
String sourceDir = "D:\\foldercopyanddel\\sourcedir\\booktest_stop.xml";
deleteFile(sourceDir);
System.out.print("deleteFile end!");
}
}