Java文件操作增强工具
生活随笔
收集整理的這篇文章主要介紹了
Java文件操作增强工具
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java的文件操作太基礎飛鴿傳書,缺乏很多實用工具,比如對目錄的操作,支持就非常的差了。如果你經常用Java操作文件或文件夾,你會覺得反復編寫這些代碼是令人沮喪的問題,而且要大量用到遞歸。
下面是的一個解決方案,借助Apache Commons IO工具包來簡單實現文件(夾)的復制、移動、刪除、獲取大小等操作,沒有經過嚴格測試,發現問題了請留言給我。
package zzvcom.cms.ccm.commons;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.IOException;
/**
* 文件工具箱
*
* @author leizhimin 2008-12-13 21:12:58
*/
public final class FileTookit {
????????private static final Log log = LogFactory.getLog(FileTookit.class);
????????/**
???????? * 復制文件或者目錄,復制前后文件完全一樣。
???????? *
???????? * @param resFilePath 源文件路徑
???????? * @param distFolder????目標文件夾
???????? * @IOException 當操作發生異常時拋出
???????? */
????????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);
????????????????}
????????}
????????/**
???????? * 刪除一個文件或者目錄
???????? *
???????? * @param targetPath 文件或者目錄路徑
???????? * @IOException 當操作發生異常時拋出
???????? */
????????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();
????????????????}
????????}
????????/**
???????? * 移動文件或者目錄,移動前后文件完全一樣,如果目標文件夾不存在則創建。
???????? *
???????? * @param resFilePath 源文件路徑
???????? * @param distFolder????目標文件夾
???????? * @IOException 當操作發生異常時拋出
???????? */
????????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 操作成功標識
???????? */
????????public static boolean renameFile(String resFilePath, String newFileName) {
????????????????String parentFilePath = new File(resFilePath).getParent();
????????????????String newFilePath = StringTookit.formatPath(parentFilePath + "/" + newFileName);
????????????????File resFile = new File(resFilePath);
????????????????File newFile = new File(newFilePath);
????????????????return resFile.renameTo(newFile);
????????}
????????/**
???????? * 讀取文件或者目錄的大小
???????? *
???????? * @param distFilePath 目標文件或者文件夾
???????? * @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;
????????}
}
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.IOException;
/**
* 文件工具箱
*
* @author leizhimin 2008-12-13 21:12:58
*/
public final class FileTookit {
????????private static final Log log = LogFactory.getLog(FileTookit.class);
????????/**
???????? * 復制文件或者目錄,復制前后文件完全一樣。
???????? *
???????? * @param resFilePath 源文件路徑
???????? * @param distFolder????目標文件夾
???????? * @IOException 當操作發生異常時拋出
???????? */
????????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);
????????????????}
????????}
????????/**
???????? * 刪除一個文件或者目錄
???????? *
???????? * @param targetPath 文件或者目錄路徑
???????? * @IOException 當操作發生異常時拋出
???????? */
????????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();
????????????????}
????????}
????????/**
???????? * 移動文件或者目錄,移動前后文件完全一樣,如果目標文件夾不存在則創建。
???????? *
???????? * @param resFilePath 源文件路徑
???????? * @param distFolder????目標文件夾
???????? * @IOException 當操作發生異常時拋出
???????? */
????????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 操作成功標識
???????? */
????????public static boolean renameFile(String resFilePath, String newFileName) {
????????????????String parentFilePath = new File(resFilePath).getParent();
????????????????String newFilePath = StringTookit.formatPath(parentFilePath + "/" + newFileName);
????????????????File resFile = new File(resFilePath);
????????????????File newFile = new File(newFilePath);
????????????????return resFile.renameTo(newFile);
????????}
????????/**
???????? * 讀取文件或者目錄的大小
???????? *
???????? * @param distFilePath 目標文件或者文件夾
???????? * @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;
????????}
}
總結
以上是生活随笔為你收集整理的Java文件操作增强工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Delphi中TWebBrowser中注
- 下一篇: java美元兑换,(Java实现) 美元