JAVA中使用File类批量重命名文件及java.io.File的常见用法

冷不防 2022-09-19 14:24 268阅读 0赞

在线文档:http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/io/File.html

构造方法如下:
















File(File parent, String child) 
          根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
File(String pathname) 
          通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File(String parent, String child) 
          根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
File(URI uri) 
          通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例。

代码如下:

  1. package com.file;
  2. import java.io.File;
  3. public class ChangeFileName {
  4. public static void main(String args[]) {
  5. File fl = new File("d://文件夹"); // 这里写上发替换的文件夹路径,注意使用双斜杠
  6. String[] files = fl.list();
  7. File f = null;
  8. String filename = "";
  9. for(String file:files){
  10. /*
  11. * 注意,这里一定要写成File(fl,file)
  12. * 如果写成File(file)是行不通的,
  13. * 一定要全路径
  14. */
  15. f = new File(fl,file);
  16. filename = f.getName();
  17. //System.out.println(filename);
  18. /*
  19. * 这里可以反复使用replace替换,
  20. * 当然也可以使用正则表达式来替换了
  21. */
  22. f.renameTo(new File(fl.getAbsolutePath()+"//"+filename.replace("要替换掉的内容", "替换成的内容")));
  23. }
  24. }
  25. }
  26. import java.io.*;
  27. public class FileOperate {
  28. public FileOperate() {
  29. }
  30. /**
  31. * 新建目录
  32. * @param folderPath String 如 c:/fqf
  33. * @return boolean
  34. */
  35. public void newFolder(String folderPath) {
  36. try {
  37. String filePath = folderPath;
  38. filePath = filePath.toString();
  39. java.io.File myFilePath = new java.io.File(filePath);
  40. if (!myFilePath.exists()) {
  41. myFilePath.mkdir();
  42. }
  43. }
  44. catch (Exception e) {
  45. System.out.println("新建目录操作出错");
  46. e.printStackTrace();
  47. }
  48. }
  49. /**
  50. * 新建文件
  51. * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
  52. * @param fileContent String 文件内容
  53. * @return boolean
  54. */
  55. public void newFile(String filePathAndName, String fileContent) {
  56. try {
  57. String filePath = filePathAndName;
  58. filePath = filePath.toString();
  59. File myFilePath = new File(filePath);
  60. if (!myFilePath.exists()) {
  61. myFilePath.createNewFile();
  62. }
  63. FileWriter resultFile = new FileWriter(myFilePath);
  64. PrintWriter myFile = new PrintWriter(resultFile);
  65. String strContent = fileContent;
  66. myFile.println(strContent);
  67. resultFile.close();
  68. }
  69. catch (Exception e) {
  70. System.out.println("新建目录操作出错");
  71. e.printStackTrace();
  72. }
  73. }
  74. /**
  75. * 删除文件
  76. * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
  77. * @param fileContent String
  78. * @return boolean
  79. */
  80. public void delFile(String filePathAndName) {
  81. try {
  82. String filePath = filePathAndName;
  83. filePath = filePath.toString();
  84. java.io.File myDelFile = new java.io.File(filePath);
  85. myDelFile.delete();
  86. }
  87. catch (Exception e) {
  88. System.out.println("删除文件操作出错");
  89. e.printStackTrace();
  90. }
  91. }
  92. /**
  93. * 删除文件夹
  94. * @param filePathAndName String 文件夹路径及名称 如c:/fqf
  95. * @param fileContent String
  96. * @return boolean
  97. */
  98. public void delFolder(String folderPath) {
  99. try {
  100. delAllFile(folderPath); //删除完里面所有内容
  101. String filePath = folderPath;
  102. filePath = filePath.toString();
  103. java.io.File myFilePath = new java.io.File(filePath);
  104. myFilePath.delete(); //删除空文件夹
  105. }
  106. catch (Exception e) {
  107. System.out.println("删除文件夹操作出错");
  108. e.printStackTrace();
  109. }
  110. }
  111. /**
  112. * 删除文件夹里面的所有文件
  113. * @param path String 文件夹路径 如 c:/fqf
  114. */
  115. public void delAllFile(String path) {
  116. File file = new File(path);
  117. if (!file.exists()) {
  118. return;
  119. }
  120. if (!file.isDirectory()) {
  121. return;
  122. }
  123. String[] tempList = file.list();
  124. File temp = null;
  125. for (int i = 0; i < tempList.length; i++) {
  126. if (path.endsWith(File.separator)) {
  127. temp = new File(path + tempList[i]);
  128. }
  129. else {
  130. temp = new File(path + File.separator + tempList[i]);
  131. }
  132. if (temp.isFile()) {
  133. temp.delete();
  134. }
  135. if (temp.isDirectory()) {
  136. delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
  137. delFolder(path+"/"+ tempList[i]);//再删除空文件夹
  138. }
  139. }
  140. }
  141. /**
  142. * 复制单个文件
  143. * @param oldPath String 原文件路径 如:c:/fqf.txt
  144. * @param newPath String 复制后路径 如:f:/fqf.txt
  145. * @return boolean
  146. */
  147. public void copyFile(String oldPath, String newPath) {
  148. try {
  149. int bytesum = 0;
  150. int byteread = 0;
  151. File oldfile = new File(oldPath);
  152. if (oldfile.exists()) { //文件存在时
  153. InputStream inStream = new FileInputStream(oldPath); //读入原文件
  154. FileOutputStream fs = new FileOutputStream(newPath);
  155. byte[] buffer = new byte[1444];
  156. int length;
  157. while ( (byteread = inStream.read(buffer)) != -1) {
  158. bytesum += byteread; //字节数 文件大小
  159. System.out.println(bytesum);
  160. fs.write(buffer, 0, byteread);
  161. }
  162. inStream.close();
  163. }
  164. }
  165. catch (Exception e) {
  166. System.out.println("复制单个文件操作出错");
  167. e.printStackTrace();
  168. }
  169. }
  170. /**
  171. * 复制整个文件夹内容
  172. * @param oldPath String 原文件路径 如:c:/fqf
  173. * @param newPath String 复制后路径 如:f:/fqf/ff
  174. * @return boolean
  175. */
  176. public void copyFolder(String oldPath, String newPath) {
  177. try {
  178. (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
  179. File a=new File(oldPath);
  180. String[] file=a.list();
  181. File temp=null;
  182. for (int i = 0; i < file.length; i++) {
  183. if(oldPath.endsWith(File.separator)){
  184. temp=new File(oldPath+file[i]);
  185. }
  186. else{
  187. temp=new File(oldPath+File.separator+file[i]);
  188. }
  189. if(temp.isFile()){
  190. FileInputStream input = new FileInputStream(temp);
  191. FileOutputStream output = new FileOutputStream(newPath + "/" +
  192. (temp.getName()).toString());
  193. byte[] b = new byte[1024 * 5];
  194. int len;
  195. while ( (len = input.read(b)) != -1) {
  196. output.write(b, 0, len);
  197. }
  198. output.flush();
  199. output.close();
  200. input.close();
  201. }
  202. if(temp.isDirectory()){//如果是子文件夹
  203. copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
  204. }
  205. }
  206. }
  207. catch (Exception e) {
  208. System.out.println("复制整个文件夹内容操作出错");
  209. e.printStackTrace();
  210. }
  211. }
  212. /**
  213. * 移动文件到指定目录
  214. * @param oldPath String 如:c:/fqf.txt
  215. * @param newPath String 如:d:/fqf.txt
  216. */
  217. public void moveFile(String oldPath, String newPath) {
  218. copyFile(oldPath, newPath);
  219. delFile(oldPath);
  220. }
  221. /**
  222. * 移动文件到指定目录
  223. * @param oldPath String 如:c:/fqf.txt
  224. * @param newPath String 如:d:/fqf.txt
  225. */
  226. public void moveFolder(String oldPath, String newPath) {
  227. copyFolder(oldPath, newPath);
  228. delFolder(oldPath);
  229. }
  230. }

发表评论

表情:
评论列表 (有 0 条评论,268人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JAVAFile

    序可能经常需要获取磁盘上文件的有关信息或是在磁盘上创建新的文件等,这就需要使用File类。需要注意的是,File类的对象主要用来获取文件本身的一些信息,例如文件所在的目录...

    相关 批量命名大量文件

    参考自《linux shell 脚本攻略(第2版)》 将一个文件夹下所有后缀为JPEG的文件重命名为后缀为jpg的文件,文件名不变。 当文件夹下的文件数量太多时,使用普通的

    相关 Python - 批量文件命名

      两个目标两个: 1. 输入一组文件名,进行批量重命名; 2. 输入一组目录名,批量重命名各个目录下的文件。 附加功能: 1. 可根据文件的创建日期对文件重新排序;

    相关 JAVA批量命名

    起因 原文件名太过于冗长,而且看起来肥肠的不爽,于是就想把它改掉!!! ![70][] 改完之后,现在这样就爽多了!!! ![70 1][] 代码