[Java学习] Java文件操作工具类

移动目录或者文件, 将一个文件夹下所有文件移动到另一个文件夹下,将指定文件移动到目的目录,刪除指定目录和其下所有子目录和文件,如果指定的是文件则直接删除
  1. import java.io.File;
  2. /**
  3. *
  4. * @Title : FileUtils
  5. * @File Name : FileUtils.java
  6. * @Description : 文件操作工具类
  7. * @Date : 2014年8月10日
  8. * @author : 王鸿运
  9. * @version : 1.0
  10. * @Others :
  11. * @History 1.<br/>
  12. * Date : <br/>
  13. * Author : <br/>
  14. * Modification: <br/>
  15. */
  16. public class FileUtils {
  17. public static final String FILE_SEP = System.getProperty("file.separator");
  18. http://www.kmnk01.com/hxpfk/2015/qb_1216/305.html
  19. /**
  20. *
  21. * @Function : moveFile
  22. * @Desc : 移动目录或者文件
  23. * @Author : 王鸿运
  24. * @param srcFilePath 源文件(目录)路径
  25. * @param destFilePath 目的文件(目录)路径
  26. * @return true:操作成功,false:操作失败
  27. */
  28. public static boolean moveFile(String srcFilePath, String destFilePath) {
  29. File srcFile = new File(srcFilePath);
  30. if (!srcFile.exists()) {
  31. return false;
  32. }
  33. if (srcFile.isDirectory()) {
  34. File[] subFiles = srcFile.listFiles();
  35. for (File file : subFiles) {
  36. moveFile(file.getAbsolutePath(), destFilePath +FILE_SEP+ file.getName());
  37. }
  38. }
  39. File destFile = new File(destFilePath);http://www.kmnk01.com/hxpfk/2015/qb_1216/306.html
  40. File destParentFile = destFile.getParentFile();
  41. if (!destParentFile.exists()) {
  42. destParentFile.mkdirs();
  43. }
  44. srcFile.renameTo(destFile);
  45. return true;
  46. }
  47. http://www.kmnk01.com/hxpfk/2015/qb_1216/307.html
  48. /**
  49. * @Function : moveChildren
  50. * @Desc : 指定目录下所有的子目录和文件到目的目录
  51. * @Author : 王鸿运
  52. * @param srcFilePath 源目录路径
  53. * @param destFilePath 目的路径
  54. * @return true:操作成功,false:操作失败
  55. */
  56. public static boolean moveAllChildren(String srcFilePath, String destFilePath) {
  57. File srcDir = new File(srcFilePath);
  58. if (!srcDir.exists() || !srcDir.isDirectory()) {
  59. return false;
  60. }
  61. File destDir = new File(destFilePath);
  62. if (destDir.exists()) {
  63. if ( destDir.isFile()) {
  64. destDir.delete();
  65. destDir = new File(destFilePath);
  66. destDir.mkdir();
  67. }
  68. } else {
  69. destDir.mkdirs();
  70. }
  71. http://www.kmnk01.com/hxpfk/2015/qb_1216/309.html
  72. File[] children = srcDir.listFiles();
  73. for (File file : children) {
  74. moveFile(file.getAbsolutePath(), destFilePath);
  75. }
  76. http://www.kmnk03.com/hxpfk/bdf/323.html
  77. return true;
  78. }
  79. /**
  80. *
  81. * @Function : moveFiles
  82. * @Desc : 将指定文件移动到目的目录
  83. * @Author : 王鸿运
  84. * @param srcFiles 源文件,支持多个路径参数
  85. * @param destDirPath 目的目录
  86. * @return true:操作成功,false:操作失败
  87. */
  88. public static boolean moveFiles(String destDirPath, String ... srcFiles){
  89. if (srcFiles == null || srcFiles.length == 0) {
  90. return false;
  91. }
  92. File destDir = new File(destDirPath);
  93. http://www.kmnk01.com/hxpfk/2015/qb_1216/311.html
  94. boolean destExists = true;
  95. if (!destDir.exists()) {
  96. destExists = destDir.mkdirs();
  97. } else if(destDir.isFile()){
  98. return false;
  99. }
  100. if (!destExists) {
  101. return false;
  102. }
  103. for (String srcFilePath : srcFiles) {
  104. File srcFile = new File(srcFilePath);
  105. if (srcFile.exists() && srcFile.isFile()) {
  106. srcFile.renameTo(new File(destDirPath + FILE_SEP + srcFile.getName()));
  107. }
  108. }
  109. return true;
  110. }
  111. /**
  112. *
  113. * @Function : deleteSelfAndAllChildren
  114. * @Desc : 刪除指定目录和其下所有子目录和文件,如果指定的是文件则直接删除
  115. * @Author : 王鸿运
  116. * @param dirPath 源目录或文件路径
  117. * @return true:删除成功,false:删除失败
  118. */http://www.kmnk03.com/hxpfk/npx/318.html
  119. public static boolean deleteSelfAndAllChildren(String dirPath) {
  120. File srcFile = new File(dirPath);
  121. if (!srcFile.exists()) {
  122. return false;
  123. }
  124. if (srcFile.isFile()) {
  125. srcFile.delete();
  126. } else {
  127. File[] children = srcFile.listFiles();
  128. http://www.kmnk03.com/hxpfk/bdf/320.html
  129. if (children.length == 0) {
  130. srcFile.delete();
  131. } else {
  132. for (File file : children) {//先删除子文件和目录
  133. deleteSelfAndAllChildren(file.getAbsolutePath());
  134. }
  135. //再删除本身
  136. srcFile.delete();
  137. }
  138. }
  139. return true;
  140. }
  141. /**
  142. *
  143. * @Function : deleteAllChildren
  144. * @Desc : 删除一个目录下所有的子目录和文件
  145. * @Author : 王鸿运
  146. * @param dirPath
  147. * @return true:删除成功,false:删除失败
  148. */http://www.kmnk03.com/hxpfk/bdf/321.html
  149. public static boolean deleteAllChildren(String dirPath){
  150. File dir = new File(dirPath);
  151. if (!dir.exists()) {
  152. return false;http://www.kmnk03.com/hxpfk/bdf/322.html
  153. }
  154. File[] children = dir.listFiles();
  155. for (File file : children) {
  156. deleteSelfAndAllChildren(file.getAbsolutePath());
  157. }
  158. return true;
  159. }
  160. }kmnk03.com   kmnk01.com
  161. }www.kmnk03.com   www.kmnk01.com
package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值