Java创建、重命名、删除文件和文件夹

来源:互联网 发布:枯枝败叶 知乎 编辑:程序博客网 时间:2024/05/17 09:40

Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了。如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归。

下面是的一个解决方案,借助Apache Commons IO工具包(commons-io-2.3.jar)来简单实现文件(夹)的复制、移动、删除、获取大小等操作。

Apache Commons IO工具包下载路径:

http://commons.apache.org/io/download_io.cgi


[java] view plaincopyprint?
  1. import org.apache.commons.io.FileUtils;  
  2. import org.apache.commons.io.filefilter.*;  
  3. import org.apache.commons.logging.Log;  
  4. import org.apache.commons.logging.LogFactory;  
  5. import java.io.*;  
  6.   
  7. /** 
  8.  * 文件工具箱 
  9.  *  
  10.  * @author leizhimin 2008-12-15 13:59:16 
  11.  */  
  12. public final class FileToolkit {  
  13.     private static final Log log = LogFactory.getLog(FileToolkit.class);  
  14.   
  15.     /** 
  16.      * 复制文件或者目录,复制前后文件完全一样。 
  17.      *  
  18.      * @param resFilePath 
  19.      *            源文件路径 
  20.      * @param distFolder 
  21.      *            目标文件夹 
  22.      * @IOException 当操作发生异常时抛出 
  23.      */  
  24.     public static void copyFile(String resFilePath, String distFolder)  
  25.             throws IOException {  
  26.         File resFile = new File(resFilePath);  
  27.         File distFile = new File(distFolder);  
  28.         if (resFile.isDirectory()) {  
  29.             FileUtils.copyDirectoryToDirectory(resFile, distFile);  
  30.         } else if (resFile.isFile()) {  
  31.             FileUtils.copyFileToDirectory(resFile, distFile, true);  
  32.         }  
  33.     }  
  34.   
  35.     /** 
  36.      * 删除一个文件或者目录 
  37.      *  
  38.      * @param targetPath 
  39.      *            文件或者目录路径 
  40.      * @IOException 当操作发生异常时抛出 
  41.      */  
  42.     public static void deleteFile(String targetPath) throws IOException {  
  43.         File targetFile = new File(targetPath);  
  44.         if (targetFile.isDirectory()) {  
  45.             FileUtils.deleteDirectory(targetFile);  
  46.         } else if (targetFile.isFile()) {  
  47.             targetFile.delete();  
  48.         }  
  49.     }  
  50.   
  51.     /** 
  52.      * 移动文件或者目录,移动前后文件完全一样,如果目标文件夹不存在则创建。 
  53.      *  
  54.      * @param resFilePath 
  55.      *            源文件路径 
  56.      * @param distFolder 
  57.      *            目标文件夹 
  58.      * @IOException 当操作发生异常时抛出 
  59.      */  
  60.     public static void moveFile(String resFilePath, String distFolder)  
  61.             throws IOException {  
  62.         File resFile = new File(resFilePath);  
  63.         File distFile = new File(distFolder);  
  64.         if (resFile.isDirectory()) {  
  65.             FileUtils.moveDirectoryToDirectory(resFile, distFile, true);  
  66.         } else if (resFile.isFile()) {  
  67.             FileUtils.moveFileToDirectory(resFile, distFile, true);  
  68.         }  
  69.     }  
  70.   
  71.     /** 
  72.      * 重命名文件或文件夹 
  73.      *  
  74.      * @param resFilePath 
  75.      *            源文件路径 
  76.      * @param newFileName 
  77.      *            重命名 
  78.      * @return 操作成功标识 
  79.      */  
  80.     public static boolean renameFile(String resFilePath, String newFileName) {  
  81.         String newFilePath = StringToolkit.formatPath(StringToolkit  
  82.                 .getParentPath(resFilePath) + "/" + newFileName);  
  83.         File resFile = new File(resFilePath);  
  84.         File newFile = new File(newFilePath);  
  85.         return resFile.renameTo(newFile);  
  86.     }  
  87.   
  88.     /** 
  89.      * 读取文件或者目录的大小 
  90.      *  
  91.      * @param distFilePath 
  92.      *            目标文件或者文件夹 
  93.      * @return 文件或者目录的大小,如果获取失败,则返回-1 
  94.      */  
  95.     public static long genFileSize(String distFilePath) {  
  96.         File distFile = new File(distFilePath);  
  97.         if (distFile.isFile()) {  
  98.             return distFile.length();  
  99.         } else if (distFile.isDirectory()) {  
  100.             return FileUtils.sizeOfDirectory(distFile);  
  101.         }  
  102.         return -1L;  
  103.     }  
  104.   
  105.     /** 
  106.      * 判断一个文件是否存在 
  107.      *  
  108.      * @param filePath 
  109.      *            文件路径 
  110.      * @return 存在返回true,否则返回false 
  111.      */  
  112.     public static boolean isExist(String filePath) {  
  113.         return new File(filePath).exists();  
  114.     }  
  115.   
  116.     /** 
  117.      * 本地某个目录下的文件列表(不递归) 
  118.      *  
  119.      * @param folder 
  120.      *            ftp上的某个目录 
  121.      * @param suffix 
  122.      *            文件的后缀名(比如.mov.xml) 
  123.      * @return 文件名称列表 
  124.      */  
  125.     public static String[] listFilebySuffix(String folder, String suffix) {  
  126.         IOFileFilter fileFilter1 = new SuffixFileFilter(suffix);  
  127.         IOFileFilter fileFilter2 = new NotFileFilter(  
  128.                 DirectoryFileFilter.INSTANCE);  
  129.         FilenameFilter filenameFilter = new AndFileFilter(fileFilter1,  
  130.                 fileFilter2);  
  131.         return new File(folder).list(filenameFilter);  
  132.     }  
  133.   
  134.     /** 
  135.      * 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!) 
  136.      *  
  137.      * @param res 
  138.      *            原字符串 
  139.      * @param filePath 
  140.      *            文件路径 
  141.      * @return 成功标记 
  142.      */  
  143.     public static boolean string2File(String res, String filePath) {  
  144.         boolean flag = true;  
  145.         BufferedReader bufferedReader = null;  
  146.         BufferedWriter bufferedWriter = null;  
  147.         try {  
  148.             File distFile = new File(filePath);  
  149.             if (!distFile.getParentFile().exists())  
  150.                 distFile.getParentFile().mkdirs();  
  151.             bufferedReader = new BufferedReader(new StringReader(res));  
  152.             bufferedWriter = new BufferedWriter(new FileWriter(distFile));  
  153.             char buf[] = new char[1024]; // 字符缓冲区  
  154.             int len;  
  155.             while ((len = bufferedReader.read(buf)) != -1) {  
  156.                 bufferedWriter.write(buf, 0, len);  
  157.             }  
  158.             bufferedWriter.flush();  
  159.             bufferedReader.close();  
  160.             bufferedWriter.close();  
  161.         } catch (IOException e) {  
  162.             flag = false;  
  163.             e.printStackTrace();  
  164.         }  
  165.         return flag;  
  166.     }  
  167. }  
0 0
原创粉丝点击