免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2065 | 回复: 1
打印 上一主题 下一主题

java 文件工具类 FileUtil 备忘笔记 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-11 18:39 |只看该作者 |倒序浏览
java 文件工具类 FileUtil 备忘笔记









Java代码
  1. 1.package com.woyo.utils;   
  2. 2.  
  3. 3.import java.io.File;   
  4. 4.import java.io.FileInputStream;   
  5. 5.import java.io.FileOutputStream;   
  6. 6.import java.io.FileWriter;   
  7. 7.import java.io.InputStream;   
  8. 8.import java.io.PrintWriter;   
  9. 9.import java.util.HashSet;   
  10. 10.import java.util.Set;   
  11. 11.  
  12. 12.import org.apache.log4j.Logger;   
  13. 13./**  
  14. 14. * 过滤指路歌曲文件  
  15. 15. * @author dylan_xu  
  16. 16. * @date Mar 11, 2012  
  17. 17. * @modified by  
  18. 18. * @modified date  
  19. 19. * @since JDK1.6  
  20. 20. * @see com.woyo.utils.FileUtil  
  21. 21. */  
  22. 22.public class FileUtil {   
  23. 23.    public static Logger logger = Logger.getLogger(FileUtil.class);   
  24. 24.    public static Set<String> sets = new HashSet<String>();   
  25. 25.  
  26. 26.    public static void main(String[] args) {   
  27. 27.        refreshFileList("G:\\Music");   
  28. 28.        //moveFolder("G:\\music\\周杰伦", "E:\\Kugou");   
  29. 29.    }   
  30. 30.  
  31. 31.    /**  
  32. 32.     * 过滤MP3文件  
  33. 33.     *   
  34. 34.     * @param strPath  
  35. 35.     */  
  36. 36.    public static void refreshFileList(String strPath) {   
  37. 37.        File dir = new File(strPath);   
  38. 38.        File[] files = dir.listFiles();   
  39. 39.        if (files == null) {   
  40. 40.            return;   
  41. 41.        }   
  42. 42.        for (int i = 0; i < files.length; i++) {   
  43. 43.            if (files[i].isDirectory()) {   
  44. 44.                refreshFileList(files[i].getAbsolutePath());   
  45. 45.            } else {   
  46. 46.                String strFilePath = files[i].getAbsolutePath().toLowerCase();   
  47. 47.                String strName = files[i].getName();   
  48. 48.                if (strName.endsWith(".mp3")) {   
  49. 49.                    boolean bFlag = sets.add(strName);   
  50. 50.                    if (bFlag == Boolean.FALSE) {   
  51. 51.                        // 删除重复文件   
  52. 52.                        removeFile(strFilePath);   
  53. 53.                    }   
  54. 54.                }   
  55. 55.                // System.out.println("FILE_PATH:" + strFilePath + "|strName:" +   
  56. 56.                // strName);   
  57. 57.            }   
  58. 58.        }   
  59. 59.    }   
  60. 60.  
  61. 61.    /**  
  62. 62.     * 创建文件夹  
  63. 63.     *   
  64. 64.     * @param strFilePath  
  65. 65.     *            文件夹路径  
  66. 66.     */  
  67. 67.    public boolean mkdirFolder(String strFilePath) {   
  68. 68.        boolean bFlag = false;   
  69. 69.        try {   
  70. 70.            File file = new File(strFilePath.toString());   
  71. 71.            if (!file.exists()) {   
  72. 72.                bFlag = file.mkdir();   
  73. 73.            }   
  74. 74.        } catch (Exception e) {   
  75. 75.            logger.error("新建目录操作出错" + e.getLocalizedMessage());   
  76. 76.            e.printStackTrace();   
  77. 77.        }   
  78. 78.        return bFlag;   
  79. 79.    }   
  80. 80.  
  81. 81.    public boolean createFile(String strFilePath, String strFileContent) {   
  82. 82.        boolean bFlag = false;   
  83. 83.        try {   
  84. 84.            File file = new File(strFilePath.toString());   
  85. 85.            if (!file.exists()) {   
  86. 86.                bFlag = file.createNewFile();   
  87. 87.            }   
  88. 88.            if (bFlag == Boolean.TRUE) {   
  89. 89.                FileWriter fw = new FileWriter(file);   
  90. 90.                PrintWriter pw = new PrintWriter(fw);   
  91. 91.                pw.println(strFileContent.toString());   
  92. 92.                pw.close();   
  93. 93.            }   
  94. 94.        } catch (Exception e) {   
  95. 95.            logger.error("新建文件操作出错" + e.getLocalizedMessage());   
  96. 96.            e.printStackTrace();   
  97. 97.        }   
  98. 98.        return bFlag;   
  99. 99.    }   
  100. 100.  
  101. 101.    /**  
  102. 102.     * 删除文件  
  103. 103.     *   
  104. 104.     * @param strFilePath  
  105. 105.     * @return  
  106. 106.     */  
  107. 107.    public static boolean removeFile(String strFilePath) {   
  108. 108.        boolean result = false;   
  109. 109.        if (strFilePath == null || "".equals(strFilePath)) {   
  110. 110.            return result;   
  111. 111.        }   
  112. 112.        File file = new File(strFilePath);   
  113. 113.        if (file.isFile() && file.exists()) {   
  114. 114.            result = file.delete();   
  115. 115.            if (result == Boolean.TRUE) {   
  116. 116.                logger.debug("[REMOE_FILE:" + strFilePath + "删除成功!]");   
  117. 117.            } else {   
  118. 118.                logger.debug("[REMOE_FILE:" + strFilePath + "删除失败]");   
  119. 119.            }   
  120. 120.        }   
  121. 121.        return result;   
  122. 122.    }   
  123. 123.  
  124. 124.    /**  
  125. 125.     * 删除文件夹(包括文件夹中的文件内容,文件夹)  
  126. 126.     *   
  127. 127.     * @param strFolderPath  
  128. 128.     * @return  
  129. 129.     */  
  130. 130.    public static boolean removeFolder(String strFolderPath) {   
  131. 131.        boolean bFlag = false;   
  132. 132.        try {   
  133. 133.            if (strFolderPath == null || "".equals(strFolderPath)) {   
  134. 134.                return bFlag;   
  135. 135.            }   
  136. 136.            File file = new File(strFolderPath.toString());   
  137. 137.            bFlag = file.delete();   
  138. 138.            if (bFlag == Boolean.TRUE) {   
  139. 139.                logger.debug("[REMOE_FOLDER:" + file.getPath() + "删除成功!]");   
  140. 140.            } else {   
  141. 141.                logger.debug("[REMOE_FOLDER:" + file.getPath() + "删除失败]");   
  142. 142.            }   
  143. 143.        } catch (Exception e) {   
  144. 144.            logger.error("FLOADER_PATH:" + strFolderPath + "删除文件夹失败!");   
  145. 145.            e.printStackTrace();   
  146. 146.        }   
  147. 147.        return bFlag;   
  148. 148.    }   
  149. 149.  
  150. 150.    /**  
  151. 151.     * 移除所有文件  
  152. 152.     *   
  153. 153.     * @param strPath  
  154. 154.     */  
  155. 155.    public static void removeAllFile(String strPath) {   
  156. 156.        File file = new File(strPath);   
  157. 157.        if (!file.exists()) {   
  158. 158.            return;   
  159. 159.        }   
  160. 160.        if (!file.isDirectory()) {   
  161. 161.            return;   
  162. 162.        }   
  163. 163.        String[] fileList = file.list();   
  164. 164.        File tempFile = null;   
  165. 165.        for (int i = 0; i < fileList.length; i++) {   
  166. 166.            if (strPath.endsWith(File.separator)) {   
  167. 167.                tempFile = new File(strPath + fileList[i]);   
  168. 168.            } else {   
  169. 169.                tempFile = new File(strPath + File.separator + fileList[i]);   
  170. 170.            }   
  171. 171.            if (tempFile.isFile()) {   
  172. 172.                tempFile.delete();   
  173. 173.            }   
  174. 174.            if (tempFile.isDirectory()) {   
  175. 175.                removeAllFile(strPath + "/" + fileList[i]);// 下删除文件夹里面的文件   
  176. 176.                removeFolder(strPath + "/" + fileList[i]);// 删除文件夹   
  177. 177.            }   
  178. 178.        }   
  179. 179.    }   
  180. 180.  
  181. 181.    public static void copyFile(String oldPath, String newPath) {   
  182. 182.        try {   
  183. 183.            int bytesum = 0;   
  184. 184.            int byteread = 0;   
  185. 185.            File oldfile = new File(oldPath);   
  186. 186.            if (oldfile.exists()) { // 文件存在时   
  187. 187.                InputStream inStream = new FileInputStream(oldPath); // 读入原文件   
  188. 188.                FileOutputStream fs = new FileOutputStream(newPath);   
  189. 189.                byte[] buffer = new byte[1444];   
  190. 190.                while ((byteread = inStream.read(buffer)) != -1) {   
  191. 191.                    bytesum += byteread; // 字节数 文件大小   
  192. 192.                    System.out.println(bytesum);   
  193. 193.                    fs.write(buffer, 0, byteread);   
  194. 194.                }   
  195. 195.                inStream.close();   
  196. 196.                logger.debug("[COPY_FILE:" + oldfile.getPath() + "复制文件成功!]");   
  197. 197.            }   
  198. 198.        } catch (Exception e) {   
  199. 199.            System.out.println("复制单个文件操作出错 ");   
  200. 200.            e.printStackTrace();   
  201. 201.        }   
  202. 202.    }   
  203. 203.  
  204. 204.    public static void copyFolder(String oldPath, String newPath) {   
  205. 205.        try {   
  206. 206.            (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹   
  207. 207.            File a = new File(oldPath);   
  208. 208.            String[] file = a.list();   
  209. 209.            File temp = null;   
  210. 210.            for (int i = 0; i < file.length; i++) {   
  211. 211.                if (oldPath.endsWith(File.separator)) {   
  212. 212.                    temp = new File(oldPath + file[i]);   
  213. 213.                } else {   
  214. 214.                    temp = new File(oldPath + File.separator + file[i]);   
  215. 215.                }   
  216. 216.                if (temp.isFile()) {   
  217. 217.                    FileInputStream input = new FileInputStream(temp);   
  218. 218.                    FileOutputStream output = new FileOutputStream(newPath   
  219. 219.                            + "/ " + (temp.getName()).toString());   
  220. 220.                    byte[] b = new byte[1024 * 5];   
  221. 221.                    int len;   
  222. 222.                    while ((len = input.read(b)) != -1) {   
  223. 223.                        output.write(b, 0, len);   
  224. 224.                    }   
  225. 225.                    output.flush();   
  226. 226.                    output.close();   
  227. 227.                    input.close();   
  228. 228.                    logger.debug("[COPY_FILE:" + temp.getPath() + "复制文件成功!]");   
  229. 229.                }   
  230. 230.                if (temp.isDirectory()) {// 如果是子文件夹   
  231. 231.                    copyFolder(oldPath + "/ " + file[i], newPath + "/ "  
  232. 232.                            + file[i]);   
  233. 233.                }   
  234. 234.            }   
  235. 235.        } catch (Exception e) {   
  236. 236.            System.out.println("复制整个文件夹内容操作出错 ");   
  237. 237.            e.printStackTrace();   
  238. 238.        }   
  239. 239.    }   
  240. 240.  
  241. 241.    public static void moveFile(String oldPath, String newPath) {   
  242. 242.        copyFile(oldPath, newPath);   
  243. 243.        //removeFile(oldPath);   
  244. 244.    }   
  245. 245.  
  246. 246.    public static void moveFolder(String oldPath, String newPath) {   
  247. 247.        copyFolder(oldPath, newPath);   
  248. 248.        //removeFolder(oldPath);   
  249. 249.    }   
  250. 250.}  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-11 20:38 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP