文件工具类FileUtil

24 篇文章 0 订阅
[java]  view plain copy
  1. /* 
  2.  * FileUtil.java 
  3.  * Copyright (C) 2007-3-19  <JustinLei@gmail.com> 
  4.  * 
  5.  *        This program is free software; you can redistribute it and/or modify 
  6.  *        it under the terms of the GNU General Public License as published by 
  7.  *      the Free Software Foundation; either version 2 of the License, or 
  8.  *     (at your option) any later version. 
  9.  * 
  10.  *       This program is distributed in the hope that it will be useful, 
  11.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12.  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  13.  *        GNU General Public License for more details. 
  14.  * 
  15.  */  
  16. package org.lambdasoft.utils;  
  17. import java.io.BufferedReader;  
  18. import java.io.File;  
  19. import java.io.FileInputStream;  
  20. import java.io.FileOutputStream;  
  21. import java.io.FileReader;  
  22. import java.io.InputStream;  
  23. import java.util.Date;  
  24. import java.util.HashMap;  
  25. import java.util.Map;  
  26. import java.util.Properties;  
  27. import java.util.Random;  
  28. import java.util.Set;  
  29. import org.apache.commons.logging.Log;  
  30. import org.apache.commons.logging.LogFactory;  
  31. /** 
  32.  * 文件工具类 
  33.  *  
  34.  * @author TangLei <justinlei@gmail.com> 
  35.  * @date 2009-2-24 
  36.  */  
  37. public class FileUtil {  
  38.     private static Log log = LogFactory.getLog(FileUtil.class);  
  39.     private FileUtil() {}  
  40.       
  41.     /** 
  42.      * 获取随机的文件名称 
  43.      * @param seed    随机种子 
  44.      * @return 
  45.      */  
  46.     public static String getRandomFileName(String seed) {  
  47.         byte[] ra = new byte[100];  
  48.         new Random().nextBytes(ra);  
  49.         StringBuilder build = new StringBuilder("");  
  50.         for (int i = 0; i < ra.length; i++) {  
  51.             build.append(Byte.valueOf(ra[i]).toString());  
  52.         }  
  53.         String currentDate = Long.valueOf(new Date().getTime()).toString();  
  54.         seed = seed + currentDate + build.toString();  
  55.         return EncryptUtils.getMD5ofStr(seed).toLowerCase();  
  56.     }  
  57.       
  58.     /** 
  59.      * 列出所有当前层的文件和目录 
  60.      *  
  61.      * @param dir            目录名称 
  62.      * @return fileList    列出的文件和目录 
  63.      */  
  64.     public static File[] ls(String dir) {  
  65.         return new File(dir).listFiles();  
  66.     }  
  67.       
  68.     /** 
  69.      * 根据需要创建文件夹 
  70.      *  
  71.      * @param dirPath 文件夹路径 
  72.      * @param del    存在文件夹是否删除 
  73.      */  
  74.     public static void mkdir(String dirPath,boolean del) {  
  75.         File dir = new File(dirPath);  
  76.         if(dir.exists()) {  
  77.             if(del)  
  78.                 dir.delete();  
  79.             else return;  
  80.         }  
  81.         dir.mkdirs();  
  82.     }  
  83.       
  84.     /** 
  85.      * 删除文件和目录 
  86.      *  
  87.      * @param path 
  88.      * @throws Exception 
  89.      */  
  90.     public static void rm(String path) throws Exception{  
  91.         if(log.isDebugEnabled())  
  92.             log.debug("需要删除的文件: " + path);  
  93.         File file = new File(path);  
  94.         if(!file.exists()) {  
  95.             if(log.isWarnEnabled())  
  96.                 log.warn("文件<" + path + ">不存在");  
  97.             return;  
  98.         }  
  99.         if(file.isDirectory()) {  
  100.             File[] fileList = file.listFiles();  
  101.             if(fileList == null || fileList.length == 0) {  
  102.                 file.delete();  
  103.             } else {  
  104.                 for (File _file : fileList) {  
  105.                     rm(_file.getAbsolutePath());  
  106.                 }  
  107.             }  
  108.         file.delete();  
  109.         } else {  
  110.             file.delete();  
  111.         }  
  112.     }  
  113.       
  114.     /** 
  115.      * 移动文件 
  116.      *  
  117.      * @param source     源文件 
  118.      * @param target         目标文件 
  119.      * @param cache        文件缓存大小 
  120.      * @throws Exception 
  121.      */  
  122.     public static void mv(String source,String target,int cache) throws Exception {  
  123.         if(source.trim().equals(target.trim()))  
  124.             return;  
  125.         byte[] cached = new byte[cache];  
  126.         FileInputStream fromFile = new FileInputStream(source);  
  127.         FileOutputStream toFile = new FileOutputStream(target);  
  128.         while(fromFile.read(cached) != -1) {  
  129.             toFile.write(cached);  
  130.         }  
  131.         toFile.flush();  
  132.         toFile.close();  
  133.         fromFile.close();  
  134.         new File(source).deleteOnExit();  
  135.     }  
  136.       
  137.     /** 
  138.      * 把属性文件转换成Map 
  139.      *  
  140.      * @param propertiesFile 
  141.      * @return 
  142.      * @throws Exception 
  143.      */  
  144.     public static final Map<String, String> getPropertiesMap(String propertiesFile) throws Exception{  
  145.         Properties properties = new Properties();  
  146.         FileInputStream inputStream = new FileInputStream(propertiesFile);  
  147.         properties.load(inputStream);  
  148.         Map<String, String> map = new HashMap<String, String>();  
  149.         Set<Object> keySet = properties.keySet();  
  150.         for (Object key : keySet) {  
  151.             map.put((String)key, properties.getProperty((String)key));  
  152.         }  
  153.         return map;  
  154.     }  
  155.       
  156.     @SuppressWarnings("unchecked")  
  157.     public static final Map<String, String> getPropertiesMap(Class clazz,String fileName) throws Exception{  
  158.         Properties properties = new Properties();  
  159.         InputStream inputStream = clazz.getResourceAsStream(fileName);  
  160.         if(inputStream == null)  
  161.             inputStream = clazz.getClassLoader().getResourceAsStream(fileName);  
  162.         properties.load(inputStream);  
  163.         Map<String, String> map = new HashMap<String, String>();  
  164.         Set<Object> keySet = properties.keySet();  
  165.         for (Object key : keySet) {  
  166.             map.put((String)key, properties.getProperty((String)key));  
  167.         }  
  168.         return map;  
  169.     }  
  170.       
  171.     /** 
  172.      * 把属性文件转换成Map 
  173.      *  
  174.      * @param inputStream 
  175.      * @return 
  176.      * @throws Exception 
  177.      */  
  178.     public static final Map<String, String> getPropertiesMap(InputStream inputStream) throws Exception{  
  179.         Properties properties = new Properties();  
  180.         properties.load(inputStream);  
  181.         Map<String, String> map = new HashMap<String, String>();  
  182.         Set<Object> keySet = properties.keySet();  
  183.         for (Object key : keySet) {  
  184.             map.put((String)key, properties.getProperty((String)key));  
  185.         }  
  186.         return map;  
  187.     }  
  188.       
  189.     /** 
  190.      * 把文本文件转换成String 
  191.      *  
  192.      * @param fullPath 
  193.      * @return 
  194.      * @throws Exception 
  195.      */  
  196.     public static String readFile(String fullPath) throws Exception{  
  197.         BufferedReader reader = new BufferedReader(new FileReader(fullPath));  
  198.         if(reader == null)  
  199.             return null;  
  200.         StringBuilder builder = new StringBuilder("");  
  201.         String line = null;  
  202.         while((line = reader.readLine()) != null) {  
  203.             builder.append(line + "/n");  
  204.         }  
  205.         return builder.toString();  
  206.     }  
  207.       
  208.     /** 
  209.      * 获取资源文件流 
  210.      *  
  211.      * @param clazz 
  212.      * @param name 
  213.      * @return 
  214.      */  
  215.     @SuppressWarnings("unchecked")  
  216.     public static InputStream getResourceAsStream(Class clazz,String name) {  
  217.         try {  
  218.             InputStream inputStream = clazz.getResourceAsStream(name);  
  219.             if(inputStream == null)   
  220.                 inputStream = clazz.getClassLoader().getResourceAsStream(name);  
  221.             return inputStream;  
  222.         } catch (Exception e) {  
  223.             if(log.isWarnEnabled())  
  224.                 log.warn("获取资源文件失败", e);  
  225.             return null;  
  226.         }  
  227.     }  
  228. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值