Java文件操作增强工具
生活随笔
收集整理的這篇文章主要介紹了
Java文件操作增强工具
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java的文件操作太基礎(chǔ)飛鴿傳書(shū),缺乏很多實(shí)用工具,比如對(duì)目錄的操作,支持就非常的差了。如果你經(jīng)常用Java操作文件或文件夾,你會(huì)覺(jué)得反復(fù)編寫(xiě)這些代碼是令人沮喪的問(wèn)題,而且要大量用到遞歸。
下面是的一個(gè)解決方案,借助Apache Commons IO工具包來(lái)簡(jiǎn)單實(shí)現(xiàn)文件(夾)的復(fù)制、移動(dòng)、刪除、獲取大小等操作,沒(méi)有經(jīng)過(guò)嚴(yán)格測(cè)試,發(fā)現(xiàn)問(wèn)題了請(qǐng)留言給我。
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);
????????/**
???????? * 復(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 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 目標(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;
????????}
}
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);
????????/**
???????? * 復(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 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 目標(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;
????????}
}
總結(jié)
以上是生活随笔為你收集整理的Java文件操作增强工具的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Delphi中TWebBrowser中注
- 下一篇: Javascript中“==”与“===