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