Java文件操作

  1. import org.apache.commons.io.FileUtils; 
  2. import org.apache.commons.logging.Log; 
  3. import org.apache.commons.logging.LogFactory; 
  4. import java.io.File; 
  5. import java.io.IOException; 
  6. /** 
  7. * 文件工具箱 
  8. * @author 
  9. */ 
  10. public final class FileTookit { 
  11.         private static final Log log = LogFactory.getLog(FileTookit.class); 
  12.         /** 
  13.          * 复制文件或者目录,复制前后文件完全一样。 
  14.          * 
  15.          * @param resFilePath 源文件路径 
  16.          * @param distFolder    目标文件夹 
  17.          * @IOException 当操作发生异常时抛出 
  18.          */ 
  19.         public static void copyFile(String resFilePath, String distFolder) throws IOException { 
  20.                 File resFile = new File(resFilePath); 
  21.                 File distFile = new File(distFolder); 
  22.                 if (resFile.isDirectory()) { 
  23.                         FileUtils.copyDirectoryToDirectory(resFile, distFile); 
  24.                 } else if (resFile.isFile()) { 
  25.                         FileUtils.copyFileToDirectory(resFile, distFile, true); 
  26.                 } 
  27.         } 
  28.         /** 
  29.          * 删除一个文件或者目录 
  30.          * 
  31.          * @param targetPath 文件或者目录路径 
  32.          * @IOException 当操作发生异常时抛出 
  33.          */ 
  34.         public static void deleteFile(String targetPath) throws IOException { 
  35.                 File targetFile = new File(targetPath); 
  36.                 if (targetFile.isDirectory()) { 
  37.                         FileUtils.deleteDirectory(targetFile); 
  38.                 } else if (targetFile.isFile()) { 
  39.                         targetFile.delete(); 
  40.                 } 
  41.         } 
  42.         /** 
  43.          * 移动文件或者目录,移动前后文件完全一样,如果目标文件夹不存在则创建。 
  44.          * 
  45.          * @param resFilePath 源文件路径 
  46.          * @param distFolder    目标文件夹 
  47.          * @IOException 当操作发生异常时抛出 
  48.          */ 
  49.         public static void moveFile(String resFilePath, String distFolder) throws IOException { 
  50.                 File resFile = new File(resFilePath); 
  51.                 File distFile = new File(distFolder); 
  52.                 if (resFile.isDirectory()) { 
  53.                         FileUtils.moveDirectoryToDirectory(resFile, distFile, true); 
  54.                 } else if (resFile.isFile()) { 
  55.                         FileUtils.moveFileToDirectory(resFile, distFile, true); 
  56.                 } 
  57.         } 
  58.         /** 
  59.          * 重命名文件或文件夹 
  60.          * 
  61.          * @param resFilePath 源文件路径 
  62.          * @param newFileName 重命名 
  63.          * @return 操作成功标识 
  64.          */ 
  65.         public static boolean renameFile(String resFilePath, String newFileName) { 
  66.                 String parentFilePath = new File(resFilePath).getParent(); 
  67.                 String newFilePath = StringTookit.formatPath(parentFilePath + "/" + newFileName); 
  68.                 File resFile = new File(resFilePath); 
  69.                 File newFile = new File(newFilePath); 
  70.                 return resFile.renameTo(newFile); 
  71.         } 
  72.         /** 
  73.          * 读取文件或者目录的大小 
  74.          * 
  75.          * @param distFilePath 目标文件或者文件夹 
  76.          * @return 文件或者目录的大小,如果获取失败,则返回-1 
  77.          */ 
  78.         public static Long genFileSize(String distFilePath) { 
  79.                 File distFile = new File(distFilePath); 
  80.                 if (distFile.isFile()) { 
  81.                         return distFile.length(); 
  82.                 } else if (distFile.isDirectory()) { 
  83.                         return FileUtils.sizeOfDirectory(distFile); 
  84.                 } 
  85.                 return -1L; 
  86.         } 
  87. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值