在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java 文件操作工具类

2016-12-20 13:16| 发布者: zhangjf| 查看: 429| 评论: 0

摘要: 文件及其文件夹的复制,删除,拷贝等操作类 package util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter; public class FileUtil ...
文件及其文件夹的复制,删除,拷贝等操作类
  1. package util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.FilenameFilter;
  6. public class FileUtil {
  7. private final static String FILE_SUFFIX = ".java.drl";
  8. private final static String FILE_TEMP = "C:/temp/";
  9. /**
  10. * 将已存在的drl文件删除
  11. *
  12. * @param ObjectPath
  13. */
  14. public static void deleteExistedDRLFile(String ObjectPath) {
  15. File filePath = new File(ObjectPath);
  16. if (!filePath.exists()) {
  17. System.out.println("目录不存在!");
  18. } else {
  19. if (filePath.isDirectory()) {
  20. File[] list = filePath.listFiles(new FilenameFilter() {
  21. public boolean accept(File dir, String name) {
  22. return name.endsWith(FILE_SUFFIX);
  23. }
  24. });
  25. for (int i = 0; i < list.length; i++) {
  26. list[i].delete();
  27. }
  28. }
  29. }
  30. }
  31. /**
  32. * 创建文件夹,如果有则不创建
  33. *
  34. * @param ObjectPath
  35. */
  36. public static boolean creareDirectory(String ObjectPath) {
  37. boolean flag = true;
  38. File filePath = new File(ObjectPath);
  39. if (!filePath.exists()) {
  40. filePath.mkdir();
  41. flag = false;
  42. }
  43. return flag;
  44. }
  45. /**
  46. * 查看某文件夹下面是否有文件,有文件则创建一个temp文件夹,将文件拷贝到temp目录下(备份文件) 没有文件怎什么都不做
  47. * 备份后,把原文件夹里文件删除
  48. *
  49. * @param ObjectPath
  50. */
  51. public static void backupFile(String ObjectPath, String dirName) {
  52. String backupPath;
  53. if (!FILE_TEMP.endsWith(File.separator)) {
  54. backupPath = FILE_TEMP + File.separator + dirName;
  55. } else {
  56. backupPath = FILE_TEMP + dirName;
  57. }
  58. File backupFilePath = new File(backupPath);
  59. if (!backupFilePath.exists()) {
  60. backupFilePath.mkdirs();
  61. }
  62. File filePath = new File(ObjectPath);
  63. if (!filePath.exists()) {
  64. System.out.println("目录不存在!");
  65. } else {
  66. if (filePath.isDirectory()) {
  67. File[] list = filePath.listFiles();
  68. if (list != null && list.length != 0) {
  69. copyFolder(ObjectPath, backupPath);// 文件备份
  70. for (int i = 0; i < list.length; i++) {
  71. list[i].delete();
  72. }
  73. }
  74. }
  75. }
  76. }
  77. /**
  78. * 复原文件,把文件从备份文件夹拷贝到原来文件夹
  79. *
  80. * @param ObjectPath
  81. * @param dirName
  82. */
  83. public static void recoverFile(String ObjectPath, String dirName) {
  84. String backupPath;
  85. if (ObjectPath.endsWith(File.separator)) {
  86. ObjectPath = new StringBuffer(ObjectPath).append(dirName)
  87. .toString();
  88. } else {
  89. ObjectPath = new StringBuffer(ObjectPath)
  90. .append(File.separatorChar).append(dirName).toString();
  91. }
  92. if (!FILE_TEMP.endsWith(File.separator)) {
  93. backupPath = FILE_TEMP + File.separator + dirName;
  94. } else {
  95. backupPath = FILE_TEMP + dirName;
  96. }
  97. File backupFilePath = new File(backupPath);
  98. if (!backupFilePath.exists()) {
  99. backupFilePath.mkdirs();
  100. }
  101. File filePath = new File(ObjectPath);
  102. if (!filePath.exists()) {
  103. System.out.println("目录不存在!");
  104. } else {
  105. if (filePath.isDirectory()) {
  106. File[] list = filePath.listFiles();
  107. if (list != null && list.length != 0) {
  108. copyFolder(backupPath, ObjectPath);// 文件复原
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * 复制整个文件夹内容
  115. *
  116. * @param oldPath
  117. * String 原文件路径 如:c:/fqf
  118. * @param newPath
  119. * String 复制后路径 如:f:/fqf/ff
  120. * @return boolean
  121. */
  122. public static void copyFolder(String oldPath, String newPath) {
  123. try {
  124. (new File(newPath)).mkdir(); // 如果文件夹不存在 则建立新文件夹
  125. File a = new File(oldPath);
  126. String[] file = a.list();
  127. File temp = null;
  128. for (int i = 0; i < file.length; i++) {
  129. if (oldPath.endsWith(File.separator)) {
  130. temp = new File(oldPath + file[i]);
  131. } else {
  132. temp = new File(oldPath + File.separator + file[i]);
  133. }
  134. if (temp.isFile()) {
  135. FileInputStream input = new FileInputStream(temp);
  136. FileOutputStream output = new FileOutputStream(newPath
  137. + "/" + (temp.getName()).toString());
  138. byte[] b = new byte[1024 * 5];
  139. int len;
  140. while ((len = input.read(b)) != -1) {
  141. output.write(b, 0, len);
  142. }
  143. output.flush();
  144. output.close();
  145. input.close();
  146. }
  147. if (temp.isDirectory()) {// 如果是子文件夹
  148. copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
  149. }
  150. }
  151. } catch (Exception e) {
  152. System.out.println("复制整个文件夹内容操作出错");
  153. e.printStackTrace();
  154. }
  155. }
  156. /**
  157. * 删除备份文件和存放备份文件的文件夹
  158. *
  159. * @param ObjectPath
  160. */
  161. public static void deleteFileAndDirectory(String dirName) {
  162. String ObjectPath;
  163. if (!FILE_TEMP.endsWith(File.separator)) {
  164. ObjectPath = FILE_TEMP + File.separator + dirName;
  165. } else {
  166. ObjectPath = FILE_TEMP + dirName;
  167. }
  168. File filePath = new File(ObjectPath);
  169. if (!filePath.exists()) {
  170. filePath.mkdirs();
  171. System.out.println("目录不存在!");
  172. } else {
  173. if (filePath.isDirectory()) {
  174. File[] list = filePath.listFiles();
  175. for (int i = 0; i < list.length; i++) {
  176. list[i].delete();
  177. }
  178. }
  179. filePath.delete();
  180. }
  181. }
  182. /**
  183. * 判断某文件夹下是否存在文件,存在返回true
  184. *
  185. * @param ObjectPath
  186. * @return
  187. */
  188. public static boolean existFileInDirectory(String ObjectPath) {
  189. boolean flag = false;
  190. File filePath = new File(ObjectPath);
  191. if (filePath.exists()) {
  192. if (filePath.isDirectory()) {
  193. File[] list = filePath.listFiles();
  194. if (list != null && list.length != 0) {
  195. flag = true;
  196. }
  197. }
  198. }
  199. return flag;
  200. }
  201. /**
  202. * 删除某个文件夹
  203. * @param ObjectPath
  204. */
  205. public static void deleteDirectory(String ObjectPath) {
  206. File filePath = new File(ObjectPath);
  207. if (filePath.exists()) {
  208. filePath.delete();
  209. }
  210. }
  211. /**
  212. * 将已存在的文件删除
  213. *
  214. * @param ObjectPath
  215. */
  216. public static boolean deleteExistedFile(String ObjectPath,final String fileName) {
  217. boolean flag =false;
  218. File filePath = new File(ObjectPath);
  219. if (filePath.exists()) {
  220. if (filePath.isDirectory()) {
  221. File[] list = filePath.listFiles(new FilenameFilter() {
  222. public boolean accept(File dir, String name) {
  223. return name.equals(fileName);
  224. }
  225. });
  226. for (int i = 0; i < list.length; i++) {
  227. list[i].delete();
  228. }
  229. flag=true;
  230. }
  231. }
  232. return flag;
  233. }
  234. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 12:04

Copyright 2015-2025 djqfx

返回顶部