Java的文件操作太基礎(chǔ),缺乏很多實(shí)用工具,比如對(duì)目錄的操作,支持就非常的差了。如果你經(jīng)常用Java操作文件或文件夾,你會(huì)覺(jué)得反復(fù)編寫(xiě)這些代碼是令人沮喪的問(wèn)題,而且要大量用到遞歸。
下面是的一個(gè)解決方案,借助Apache Commons IO工具包(commons-io-1.1.jar)來(lái)簡(jiǎn)單實(shí)現(xiàn)文件(夾)的復(fù)制、移動(dòng)、刪除、獲取大小等操作。
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.*;
/**
* 文件工具箱
*
* @author leizhimin 2008-12-15 13:59:16
*/
public final class FileToolkit {
private static final Log log = LogFactory.getLog(FileToolkit.class);
/**
* 復(fù)制文件或者目錄,復(fù)制前后文件完全一樣。
*
* @param resFilePath 源文件路徑
* @param distFolder 目標(biāo)文件夾
* @IOException 當(dāng)操作發(fā)生異常時(shí)拋出
*/
public static void copyFile(String resFilePath, String distFolder) throws IOException {
File resFile = new File(resFilePath);
File distFile = new File(distFolder);
if (resFile.isDirectory()) {
FileUtils.copyDirectoryToDirectory(resFile, distFile);
} else if (resFile.isFile()) {
FileUtils.copyFileToDirectory(resFile, distFile, true);
}
}
/**
* 刪除一個(gè)文件或者目錄
*
* @param targetPath 文件或者目錄路徑
* @IOException 當(dāng)操作發(fā)生異常時(shí)拋出
*/
public static void deleteFile(String targetPath) throws IOException {
File targetFile = new File(targetPath);
if (targetFile.isDirectory()) {
FileUtils.deleteDirectory(targetFile);
} else if (targetFile.isFile()) {
targetFile.delete();
}
}
/**
* 移動(dòng)文件或者目錄,移動(dòng)前后文件完全一樣,如果目標(biāo)文件夾不存在則創(chuàng)建。
*
* @param resFilePath 源文件路徑
* @param distFolder 目標(biāo)文件夾
* @IOException 當(dāng)操作發(fā)生異常時(shí)拋出
*/
public static void moveFile(String resFilePath, String distFolder) throws IOException {
File resFile = new File(resFilePath);
File distFile = new File(distFolder);
if (resFile.isDirectory()) {
FileUtils.moveDirectoryToDirectory(resFile, distFile, true);
} else if (resFile.isFile()) {
FileUtils.moveFileToDirectory(resFile, distFile, true);
}
}
/**
* 重命名文件或文件夾
*
* @param resFilePath 源文件路徑
* @param newFileName 重命名
* @return 操作成功標(biāo)識(shí)
*/
public static boolean renameFile(String resFilePath, String newFileName) {
String newFilePath = StringToolkit.formatPath(StringToolkit.getParentPath(resFilePath) + "/" + newFileName);
File resFile = new File(resFilePath);
File newFile = new File(newFilePath);
return resFile.renameTo(newFile);
}
/**
* 讀取文件或者目錄的大小
*
* @param distFilePath 目標(biāo)文件或者文件夾
* @return 文件或者目錄的大小,如果獲取失敗,則返回-1
*/
public static long genFileSize(String distFilePath) {
File distFile = new File(distFilePath);
if (distFile.isFile()) {
return distFile.length();
} else if (distFile.isDirectory()) {
return FileUtils.sizeOfDirectory(distFile);
}
return -1L;
}
/**
* 判斷一個(gè)文件是否存在
*
* @param filePath 文件路徑
* @return 存在返回true,否則返回false
*/
public static boolean isExist(String filePath) {
return new File(filePath).exists();
}
/**
* 本地某個(gè)目錄下的文件列表(不遞歸)
*
* @param folder ftp上的某個(gè)目錄
* @param suffix 文件的后綴名(比如.mov.xml)
* @return 文件名稱列表
*/
public static String[] listFilebySuffix(String folder, String suffix) {
IOFileFilter fileFilter1 = new SuffixFileFilter(suffix);
IOFileFilter fileFilter2 = new NotFileFilter(DirectoryFileFilter.INSTANCE);
FilenameFilter filenameFilter = new AndFileFilter(fileFilter1, fileFilter2);
return new File(folder).list(filenameFilter);
}
/**
* 將字符串寫(xiě)入指定文件(當(dāng)指定的父路徑中文件夾不存在時(shí),會(huì)最大限度去創(chuàng)建,以保證保存成功!)
*
* @param res 原字符串
* @param filePath 文件路徑
* @return 成功標(biāo)記
*/
public static boolean string2File(String res, String filePath) {
boolean flag = true;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File distFile = new File(filePath);
if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs();
bufferedReader = new BufferedReader(new StringReader(res));
bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; //字符緩沖區(qū)
int len;
while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
flag = false;
e.printStackTrace();
}
return flag;
}
}
-------------------------------------------------------------------------------------------------------------