免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
FILEUTILS 介紹
  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.   
  6. import java.io.*;  
  7.   
  8. /**  
  9. * 文件工具箱  
  10. *  
  11. * @author leizhimin 2008-12-15 13:59:16  
  12. */   
  13. public final class FileToolkit {   
  14.         private static final Log log = LogFactory.getLog(FileToolkit.class);  
  15.   
  16.         /**  
  17.          * 復(fù)制文件或者目錄,復(fù)制前后文件完全一樣。  
  18.          *  
  19.          * @param resFilePath 源文件路徑  
  20.          * @param distFolder    目標(biāo)文件夾  
  21.          * @IOException 當(dāng)操作發(fā)生異常時拋出  
  22.          */   
  23.         public static void copyFile(String resFilePath, String distFolder) throws IOException {   
  24.                 File resFile = new File(resFilePath);   
  25.                 File distFile = new File(distFolder);   
  26.                 if (resFile.isDirectory()) {   
  27.                         FileUtils.copyDirectoryToDirectory(resFile, distFile);   
  28.                 } else if (resFile.isFile()) {   
  29.                         FileUtils.copyFileToDirectory(resFile, distFile, true);   
  30.                 }   
  31.         }  
  32.   
  33.         /**  
  34.          * 刪除一個文件或者目錄  
  35.          *  
  36.          * @param targetPath 文件或者目錄路徑  
  37.          * @IOException 當(dāng)操作發(fā)生異常時拋出  
  38.          */   
  39.         public static void deleteFile(String targetPath) throws IOException {   
  40.                 File targetFile = new File(targetPath);   
  41.                 if (targetFile.isDirectory()) {   
  42.                         FileUtils.deleteDirectory(targetFile);   
  43.                 } else if (targetFile.isFile()) {   
  44.                         targetFile.delete();   
  45.                 }   
  46.         }  
  47.   
  48.         /**  
  49.          * 移動文件或者目錄,移動前后文件完全一樣,如果目標(biāo)文件夾不存在則創(chuàng)建。  
  50.          *  
  51.          * @param resFilePath 源文件路徑  
  52.          * @param distFolder    目標(biāo)文件夾  
  53.          * @IOException 當(dāng)操作發(fā)生異常時拋出  
  54.          */   
  55.         public static void moveFile(String resFilePath, String distFolder) throws IOException {   
  56.                 File resFile = new File(resFilePath);   
  57.                 File distFile = new File(distFolder);   
  58.                 if (resFile.isDirectory()) {   
  59.                         FileUtils.moveDirectoryToDirectory(resFile, distFile, true);   
  60.                 } else if (resFile.isFile()) {   
  61.                         FileUtils.moveFileToDirectory(resFile, distFile, true);   
  62.                 }   
  63.         }  
  64.   
  65.   
  66.         /**  
  67.          * 重命名文件或文件夾  
  68.          *  
  69.          * @param resFilePath 源文件路徑  
  70.          * @param newFileName 重命名  
  71.          * @return 操作成功標(biāo)識  
  72.          */   
  73.         public static boolean renameFile(String resFilePath, String newFileName) {   
  74.                 String newFilePath = StringToolkit.formatPath(StringToolkit.getParentPath(resFilePath) + "/" + newFileName);   
  75.                 File resFile = new File(resFilePath);   
  76.                 File newFile = new File(newFilePath);   
  77.                 return resFile.renameTo(newFile);   
  78.         }  
  79.   
  80.         /**  
  81.          * 讀取文件或者目錄的大小  
  82.          *  
  83.          * @param distFilePath 目標(biāo)文件或者文件夾  
  84.          * @return 文件或者目錄的大小,如果獲取失敗,則返回-1  
  85.          */   
  86.         public static long genFileSize(String distFilePath) {   
  87.                 File distFile = new File(distFilePath);   
  88.                 if (distFile.isFile()) {   
  89.                         return distFile.length();   
  90.                 } else if (distFile.isDirectory()) {   
  91.                         return FileUtils.sizeOfDirectory(distFile);   
  92.                 }   
  93.                 return -1L;   
  94.         }  
  95.   
  96.         /**  
  97.          * 判斷一個文件是否存在  
  98.          *  
  99.          * @param filePath 文件路徑  
  100.          * @return 存在返回true,否則返回false  
  101.          */   
  102.         public static boolean isExist(String filePath) {   
  103.                 return new File(filePath).exists();   
  104.         }  
  105.   
  106.         /**  
  107.          * 本地某個目錄下的文件列表(不遞歸)  
  108.          *  
  109.          * @param folder ftp上的某個目錄  
  110.          * @param suffix 文件的后綴名(比如.mov.xml)  
  111.          * @return 文件名稱列表  
  112.          */   
  113.         public static String[] listFilebySuffix(String folder, String suffix) {   
  114.                 IOFileFilter fileFilter1 = new SuffixFileFilter(suffix);   
  115.                 IOFileFilter fileFilter2 = new NotFileFilter(DirectoryFileFilter.INSTANCE);   
  116.                 FilenameFilter filenameFilter = new AndFileFilter(fileFilter1, fileFilter2);   
  117.                 return new File(folder).list(filenameFilter);   
  118.         }  
  119.   
  120.         /**  
  121.          * 將字符串寫入指定文件(當(dāng)指定的父路徑中文件夾不存在時,會最大限度去創(chuàng)建,以保證保存成功!)  
  122.          *  
  123.          * @param res            原字符串  
  124.          * @param filePath 文件路徑  
  125.          * @return 成功標(biāo)記  
  126.          */   
  127.         public static boolean string2File(String res, String filePath) {   
  128.                 boolean flag = true;   
  129.                 BufferedReader bufferedReader = null;   
  130.                 BufferedWriter bufferedWriter = null;   
  131.                 try {   
  132.                         File distFile = new File(filePath);   
  133.                         if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs();   
  134.                         bufferedReader = new BufferedReader(new StringReader(res));   
  135.                         bufferedWriter = new BufferedWriter(new FileWriter(distFile));   
  136.                         char buf[] = new char[1024];         //字符緩沖區(qū)   
  137.                         int len;   
  138.                         while ((len = bufferedReader.read(buf)) != -1) {   
  139.                                 bufferedWriter.write(buf, 0, len);   
  140.                         }   
  141.                         bufferedWriter.flush();   
  142.                         bufferedReader.close();   
  143.                         bufferedWriter.close();   
  144.                 } catch (IOException e) {   
  145.                         flag = false;   
  146.                         e.printStackTrace();   
  147.                 }   
  148.                 return flag;   
  149.         }   
  150. }   
  151. -------------------------------------------------------------------------------------------------------------  
  152. import java.io.File;  
  153. import java.util.ArrayList;  
  154. import java.util.List;  
  155. import java.util.Properties;  
  156.   
  157.   
  158. /** 
  159. * 字符串工具箱 
  160. * @author leizhimin 2008-12-15 22:40:12 
  161. */  
  162. public final class StringToolkit {  
  163.   /** 
  164.   * 將一個字符串的首字母改為大寫或者小寫 
  165.   * 
  166.   * @param srcString 源字符串 
  167.   * @param flag     大小寫標(biāo)識,ture小寫,false大些 
  168.   * @return 改寫后的新字符串 
  169.   */  
  170.   public static String toLowerCaseInitial(String srcString, boolean flag) {  
  171.     StringBuilder sb = new StringBuilder();  
  172.     if (flag) {  
  173.         sb.append(Character.toLowerCase(srcString.charAt(0)));  
  174.     } else {  
  175.         sb.append(Character.toUpperCase(srcString.charAt(0)));  
  176.     }  
  177.     sb.append(srcString.substring(1));  
  178.     return sb.toString();  
  179.   }  
  180.   
  181.   /** 
  182.   * 將一個字符串按照句點(.)分隔,返回最后一段 
  183.   * 
  184.   * @param clazzName 源字符串 
  185.   * @return 句點(.)分隔后的最后一段字符串 
  186.   */  
  187.   public static String getLastName(String clazzName) {  
  188.     String[] ls = clazzName.split("\\.");  
  189.     return ls[ls.length - 1];  
  190.   }  
  191.   
  192.   /** 
  193.   * 格式化文件路徑,將其中不規(guī)范的分隔轉(zhuǎn)換為標(biāo)準(zhǔn)的分隔符,并且去掉末尾的"/"符號。 
  194.   * 
  195.   * @param path 文件路徑 
  196.   * @return 格式化后的文件路徑 
  197.   */  
  198.   public static String formatPath(String path) {  
  199.     String reg0 = "\\\\+";  
  200.     String reg = "\\\\+|/+";  
  201.     String temp = path.trim().replaceAll(reg0, "/");  
  202.     temp = temp.replaceAll(reg, "/");  
  203.     if (temp.endsWith("/")) {  
  204.         temp = temp.substring(0, temp.length() - 1);  
  205.     }  
  206.     if (System.getProperty("file.separator").equals("\\")) {  
  207.       temp= temp.replace('/','\\');  
  208.     }  
  209.     return temp;  
  210.   }  
  211.   
  212.   /** 
  213.   * 格式化文件路徑,將其中不規(guī)范的分隔轉(zhuǎn)換為標(biāo)準(zhǔn)的分隔符,并且去掉末尾的"/"符號(適用于FTP遠程文件路徑或者Web資源的相對路徑)。 
  214.   * 
  215.   * @param path 文件路徑 
  216.   * @return 格式化后的文件路徑 
  217.   */  
  218.   public static String formatPath4Ftp(String path) {  
  219.     String reg0 = "\\\\+";  
  220.     String reg = "\\\\+|/+";  
  221.     String temp = path.trim().replaceAll(reg0, "/");  
  222.     temp = temp.replaceAll(reg, "/");  
  223.     if (temp.endsWith("/")) {  
  224.         temp = temp.substring(0, temp.length() - 1);  
  225.     }  
  226.     return temp;  
  227.   }  
  228.   
  229.   public static void main(String[] args) {  
  230.     System.out.println(System.getProperty("file.separator"));  
  231.     Properties p = System.getProperties();  
  232.     System.out.println(formatPath("C:///\\xxxx\\\\\\\\\\///\\\\R5555555.txt"));  
  233.   
  234. //     List<String> result = series2List("asdf | sdf|siii|sapp|aaat| ", "\\|");  
  235. //     System.out.println(result.size());  
  236. //     for (String s : result) {  
  237. //         System.out.println(s);  
  238. //     }  
  239.   }  
  240.   
  241.   /** 
  242.   * 獲取文件父路徑 
  243.   * 
  244.   * @param path 文件路徑 
  245.   * @return 文件父路徑 
  246.   */  
  247.   public static String getParentPath(String path) {  
  248.     return new File(path).getParent();  
  249.   }  
  250.   
  251.   /** 
  252.   * 獲取相對路徑 
  253.   * 
  254.   * @param fullPath 全路徑 
  255.   * @param rootPath 根路徑 
  256.   * @return 相對根路徑的相對路徑 
  257.   */  
  258.   public static String getRelativeRootPath(String fullPath, String rootPath) {  
  259.     String relativeRootPath = null;  
  260.     String _fullPath = formatPath(fullPath);  
  261.     String _rootPath = formatPath(rootPath);  
  262.   
  263.     if (_fullPath.startsWith(_rootPath)) {  
  264.         relativeRootPath = fullPath.substring(_rootPath.length());  
  265.     } else {  
  266.         throw new RuntimeException("要處理的兩個字符串沒有包含關(guān)系,處理失??!");  
  267.     }  
  268.     if (relativeRootPath == null) return null;  
  269.     else  
  270.         return formatPath(relativeRootPath);  
  271.   }  
  272.   
  273.   /** 
  274.   * 獲取當(dāng)前系統(tǒng)換行符 
  275.   * 
  276.   * @return 系統(tǒng)換行符 
  277.   */  
  278.   public static String getSystemLineSeparator() {  
  279.     return System.getProperty("line.separator");  
  280.   }  
  281.   
  282.   /** 
  283.   * 將用“|”分隔的字符串轉(zhuǎn)換為字符串集合列表,剔除分隔后各個字符串前后的空格 
  284.   * 
  285.   * @param series 將用“|”分隔的字符串 
  286.   * @return 字符串集合列表 
  287.   */  
  288.   public static List<String> series2List(String series) {  
  289.     return series2List(series, "\\|");  
  290.   }  
  291.   
  292.   /** 
  293.   * 將用正則表達式regex分隔的字符串轉(zhuǎn)換為字符串集合列表,剔除分隔后各個字符串前后的空格 
  294.   * 
  295.   * @param series 用正則表達式分隔的字符串 
  296.   * @param regex 分隔串聯(lián)串的正則表達式 
  297.   * @return 字符串集合列表 
  298.   */  
  299.   private static List<String> series2List(String series, String regex) {  
  300.     List<String> result = new ArrayList<String>();  
  301.     if (series != null && regex != null) {  
  302.         for (String s : series.split(regex)) {  
  303.           if (s.trim() != null && !s.trim().equals("")) result.add(s.trim());  
  304.         }  
  305.     }  
  306.     return result;  
  307.   }  
  308.   
  309.   /** 
  310.   * @param strList 字符串集合列表 
  311.   * @return 通過“|”串聯(lián)為一個字符串 
  312.   */  
  313.   public static String list2series(List<String> strList) {  
  314.     StringBuffer series = new StringBuffer();  
  315.     for (String s : strList) {  
  316.         series.append(s).append("|");  
  317.     }  
  318.     return series.toString();  
  319.   }  
  320.   
  321.   /** 
  322.   * 將字符串的首字母轉(zhuǎn)為小寫 
  323.   * 
  324.   * @param resStr 源字符串 
  325.   * @return 首字母轉(zhuǎn)為小寫后的字符串 
  326.   */  
  327.   public static String firstToLowerCase(String resStr) {  
  328.     if (resStr == null) {  
  329.         return null;  
  330.     } else if ("".equals(resStr.trim())) {  
  331.         return "";  
  332.     } else {  
  333.         StringBuffer sb = new StringBuffer();  
  334.         Character c = resStr.charAt(0);  
  335.         if (Character.isLetter(c)) {  
  336.           if (Character.isUpperCase(c))  
  337.             c = Character.toLowerCase(c);  
  338.           sb.append(resStr);  
  339.           sb.setCharAt(0, c);  
  340.           return sb.toString();  
  341.         }  
  342.     }  
  343.     return resStr;  
  344.   }  
  345.   
  346.   /** 
  347.   * 將字符串的首字母轉(zhuǎn)為大寫 
  348.   * 
  349.   * @param resStr 源字符串 
  350.   * @return 首字母轉(zhuǎn)為大寫后的字符串 
  351.   */  
  352.   public static String firstToUpperCase(String resStr) {  
  353.     if (resStr == null) {  
  354.         return null;  
  355.     } else if ("".equals(resStr.trim())) {  
  356.         return "";  
  357.     } else {  
  358.         StringBuffer sb = new StringBuffer();  
  359.         Character c = resStr.charAt(0);  
  360.         if (Character.isLetter(c)) {  
  361.           if (Character.isLowerCase(c))  
  362.             c = Character.toUpperCase(c);  
  363.           sb.append(resStr);  
  364.           sb.setCharAt(0, c);  
  365.           return sb.toString();  
  366.         }  
  367.     }  
  368.     return resStr;  
  369.   }  
  370.   
  371. }  

Java的文件操作太基礎(chǔ),缺乏很多實用工具,比如對目錄的操作,支持就非常的差了。如果你經(jīng)常用Java操作文件或文件夾,你會覺得反復(fù)編寫這些代碼是令人沮喪的問題,而且要大量用到遞歸。

    下面是的一個解決方案,借助Apache Commons IO工具包(commons-io-1.1.jar)來簡單實現(xiàn)文件(夾)的復(fù)制、移動、刪除、獲取大小等操作。


本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Jdom方式進行的XML文件、Document、String之間的相互轉(zhuǎn)換
【轉(zhuǎn)】C#配置App.config
Java爬蟲的底層獲取模塊,構(gòu)造POC和漏洞檢測時常用爬蟲抓取或發(fā)送測試代碼查詢目標(biāo)站 ...
C# LINQ學(xué)習(xí)筆記三:LINQ to OBJECT之操作字符串
C#獲取文件名稱、路徑、后綴名
關(guān)于Java文件路徑問題
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服