【蛻變之路】第39天 文件工具類(2019年5月4日)

    Hello,大家好!我是程序員阿飛!今天我給大家分享一個比較好的文件工具類,雖然不是很全面,但是用起來很好。好了,開始進入我們今天的主題:文件工具類。

    

    代碼實現:

/**
* File工具類,擴展 hutool 工具包
* @author jie
* @date 2018-12-27
*/
public class FileUtil extends cn.hutool.core.io.FileUtil {

   /**
    * 定義GB的計算常量
    */
   private static final int GB = 1024 * 1024 * 1024;
   /**
    * 定義MB的計算常量
    */
   private static final int MB = 1024 * 1024;
   /**
    * 定義KB的計算常量
    */
   private static final int KB = 1024;

   /**
    * 格式化小數
    */
   private static final DecimalFormat DF = new DecimalFormat("0.00");

   /**
    * MultipartFile轉File
    * @param multipartFile
    * @return
    */
   public static File toFile(MultipartFile multipartFile){
       // 獲取文件名
       String fileName = multipartFile.getOriginalFilename();
       // 獲取文件後綴
       String prefix="."+getExtensionName(fileName);
       File file = null;
       try {
           // 用uuid作爲文件名,防止生成的臨時文件重複
           file = File.createTempFile(IdUtil.simpleUUID(), prefix);
           // MultipartFile to File
           multipartFile.transferTo(file);
       } catch (IOException e) {
           e.printStackTrace();
       }
       return file;
   }

   /**
    * 刪除
    * @param files
    */
   public static void deleteFile(File... files) {
       for (File file : files) {
           if (file.exists()) {
               file.delete();
           }
       }
   }

   /**
    * 獲取文件擴展名
    * @param filename
    * @return
    */
   public static String getExtensionName(String filename) {
       if ((filename != null) && (filename.length() > 0)) {
           int dot = filename.lastIndexOf('.');
           if ((dot >-1) && (dot < (filename.length() - 1))) {
               return filename.substring(dot + 1);
           }
       }
       return filename;
   }

   /**
    * Java文件操作 獲取不帶擴展名的文件名
    * @param filename
    * @return
    */
   public static String getFileNameNoEx(String filename) {
       if ((filename != null) && (filename.length() > 0)) {
           int dot = filename.lastIndexOf('.');
           if ((dot >-1) && (dot < (filename.length()))) {
               return filename.substring(0, dot);
           }
       }
       return filename;
   }

   /**
    * 文件大小轉換
    * @param size
    * @return
    */
   public static String getSize(int size){
       String resultSize = "";
       if (size / GB >= 1) {
           //如果當前Byte的值大於等於1GB
           resultSize = DF.format(size / (float) GB) + "GB   ";
       } else if (size / MB >= 1) {
           //如果當前Byte的值大於等於1MB
           resultSize = DF.format(size / (float) MB) + "MB   ";
       } else if (size / KB >= 1) {
           //如果當前Byte的值大於等於1KB
           resultSize = DF.format(size / (float) KB) + "KB   ";
       } else {
           resultSize = size + "B   ";
       }
       return resultSize;
   }

   public static void main(String[] args){
       String result = getExtensionName("test.txt");
       System.out.println("=========="+result);
   }
}


    【開心時刻】結婚相冊

            P012_調整大小.jpg

P015_調整大小.jpg

P018_調整大小.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章