flie工具类

package com.kedacom.meeting.weixinapi.util;

import java.io.*;

/**
 * 文件操作帮助类
 * 
 * @author wangshuxuan
 * @date 2018-9-14 下午06:37:06
 */

public class FileUtils {

   private static String FILE_SEPARATOR = "/";

   /**
    * 新建目录
    * 
    * @param path
    *            String 如 c:/fqf
    * @return boolean
    */
   public static boolean createFolder(String path) {
      try {
         File file = new File(path);
         if (!file.exists()) {
            boolean success = file.mkdirs();
            if (!success) {
               throw new Exception("创建文件目录失败");
            }
         }
         return true;
      } catch (Exception e) {
         e.printStackTrace();
         return false;
      }
   }

   /**
    * 新建文件
    * 
    * @param filePath
    *            String 文件路径及名称 如c:/fqf.txt
    * @param fileContent
    *            String 文件内容
    * @return boolean
    */
   public static boolean createFile(String filePath, String fileContent) {
      try {
         // 文件所在目录不存在则创建
         int find = filePath.lastIndexOf(FILE_SEPARATOR);
         if (find != -1) {
            String path = filePath.substring(0, find);
            createFolder(path);
         }

         // 文件不存在则创建
         File file = new File(filePath);
         if (!file.exists()) {
            boolean success = file.createNewFile();
            if (!success) {
               throw new Exception("创建文件失败");
            }
         }

         // 写入文件内容
         FileWriter resultFile = new FileWriter(file);
         PrintWriter myFile = new PrintWriter(resultFile);
         myFile.println(fileContent);
         myFile.close();
         resultFile.close();
         return true;
      } catch (Exception e) {
         e.printStackTrace();
         return false;
      }
   }

   /**
    * 删除文件
    * 
    * @param filePath
    *            String 文件路径及名称 如c:/fqf.txt
    * @return boolean
    */
   public static boolean deleteFile(String filePath) {
      try {
         File file = new File(filePath);
         boolean success = file.delete();
         if (!success) {
            throw new Exception("删除文件失败");
         }
         return true;
      } catch (Exception e) {
         e.printStackTrace();
         return false;
      }
   }

   /**
    * 删除文件夹
    * 
    * @param folderPath
    *            String 文件夹路径及名称 如c:/fqf
    * @return boolean
    */
   public static boolean deleteFolder(String folderPath) {
      try {
         return deleteAllFile(folderPath) && deleteFile(folderPath);
      } catch (Exception e) {
         e.printStackTrace();
         return false;
      }
   }

   /**
    * 删除文件夹里面的所有文件
    * 
    * @param folderPath
    *            String 文件夹路径 如 c:/fqf
    * @return boolean
    */
   public static boolean deleteAllFile(String folderPath) {
      File file = new File(folderPath);
      // 路径不存在则返回false
      if (!file.exists()) {
         return false;
      }
      // 指定路径不是目录则返回false
      if (!file.isDirectory()) {
         return false;
      }

      // 递归删除目录下的所有文件和子目录
      String[] files = file.list();
      for (int i = 0; i < files.length; i++) {
         String path = null;
         if (folderPath.endsWith(FILE_SEPARATOR)) {
            path = folderPath + files[i];
         } else {
            path = folderPath + FILE_SEPARATOR + files[i];
         }
         file = new File(path);
         if (file.isFile()) {
            boolean success = file.delete();
            if (!success) {
               return false;
            }
         } else if (file.isDirectory()) {
            // 先删除子文件夹
            deleteFolder(path);
         }
      }
      return true;
   }

   /**
    * 复制单个文件
    * 
    * @param oldPath
    *            String 原文件路径 如:c:/fqf.txt
    * @param newPath
    *            String 复制后路径 如:f:/fqf.txt
    * @return boolean
    */
   public static boolean copyFile(String oldPath, String newPath) {
      try {
         File oldfile = new File(oldPath);
         // 原文件不存在则返回false
         if (!oldfile.exists()) {
            return false;
         }

         // 文件所在目录不存在则创建
         int find = newPath.lastIndexOf(FILE_SEPARATOR);
         if (find != -1) {
            String path = newPath.substring(0, find);
            createFolder(path);
         }

         // 拷贝文件内容
         FileInputStream fis = new FileInputStream(oldPath); // 读入原文件
         FileOutputStream fos = new FileOutputStream(newPath);
         byte[] buffer = new byte[1024];
         int byteread = 0;
         while ((byteread = fis.read(buffer)) != -1) {
            fos.write(buffer, 0, byteread);
         }
         fos.flush();
         fos.close();
         fis.close();
         return true;
      } catch (Exception e) {
         e.printStackTrace();
         return false;
      }
   }

   /**
    * 复制整个文件夹内容
    * 
    * @param oldPath
    *            String 原文件路径 如:c:/fqf
    * @param newPath
    *            String 复制后路径 如:f:/fqf/ff
    * @return boolean
    */
   public static boolean copyFolder(String oldPath, String newPath) {
      try {
         File folder = new File(oldPath);
         // 如果原文件夹不存在 则返回false
         if (!folder.exists()) {
            return false;
         }

         // 如果目标文件夹不存在 则建立新文件夹
         createFolder(newPath);

         // 循环拷贝目录下的所有子目录和文件
         String[] files = folder.list();
         for (int i = 0; i < files.length; i++) {
            String path = null;
            if (oldPath.endsWith(FILE_SEPARATOR)) {
               path = oldPath + files[i];
            } else {
               path = oldPath + FILE_SEPARATOR + files[i];
            }

            File file = new File(path);
            if (file.isFile()) {
               // 拷贝文件
               FileInputStream fis = new FileInputStream(file);
               FileOutputStream fos = new FileOutputStream(newPath + FILE_SEPARATOR + file.getName());
               byte[] b = new byte[1024];
               int len;
               while ((len = fis.read(b)) != -1) {
                  fos.write(b, 0, len);
               }
               fos.flush();
               fos.close();
               fis.close();
            } else if (file.isDirectory()) {
               // 若是目录则递归拷贝目录内容
               if (oldPath.endsWith(FILE_SEPARATOR)) {
                  copyFolder(oldPath + files[i], newPath + files[i]);
               } else {
                  copyFolder(oldPath + FILE_SEPARATOR + files[i], newPath + FILE_SEPARATOR + files[i]);
               }
            }
         }
         return true;
      } catch (Exception e) {
         e.printStackTrace();
         return false;
      }
   }

   /**
    * 移动文件到指定目录
    * 
    * @param oldPath
    *            String 如:c:/fqf.txt
    * @param newPath
    *            String 如:d:/fqf.txt
    * @return boolean
    */
   public static boolean moveFile(String oldPath, String newPath) {
      return copyFile(oldPath, newPath) && deleteFile(oldPath);
   }

   /**
    * 移动文件到指定目录
    * 
    * @param oldPath
    *            String 如:c:/fqf.txt
    * @param newPath
    *            String 如:d:/fqf.txt
    * @return boolean
    */
   public static boolean moveFolder(String oldPath, String newPath) {
      return copyFolder(oldPath, newPath) && deleteFolder(oldPath);
   }

   /**
    * 格式化路径,路径中的"\\"处理为"/"
    * 
    * @param path
    * @return String
    */
   public static String formatPath(String path) {
      if (path == null) {
         return "";
      }
      return path.replaceAll("\\\\", "/");
   }

   /**
    * (用一句话描述方法的主要功能)
    * 
    * @param args
    */

   public static void main(String[] args) {
      long start = System.currentTimeMillis();
      FileUtils.createFolder("c:/test");
      // System.out.println("newFolder>>" + (System.currentTimeMillis() -
      // start));

      start = System.currentTimeMillis();
      FileUtils.createFile("c:/test/test11.txt", "test011,test012");
      FileUtils.createFile("c:/test/test1/test11.txt", "test11,test22");
      FileUtils.createFile("c:/test/test2/test22/test222.txt", "test111,test222");
      // System.out.println("newFile>>" + (System.currentTimeMillis() -
      // start));

      start = System.currentTimeMillis();
      FileUtils.moveFile("c:/test/test2/test22/test222.txt", "c:/test/test3/test33/test333.txt");
      // System.out.println("moveFile>>" + (System.currentTimeMillis() -
      // start));

      start = System.currentTimeMillis();
      FileUtils.moveFolder("c:/test", "c:/test1");
      // System.out.println("moveFolder>>" + (System.currentTimeMillis() -
      // start));
   }
}

猜你喜欢

转载自blog.csdn.net/qq_38999810/article/details/85316747