java对文件新建,删除,复制,剪切,重命名

本是古典 何须时尚 2022-08-04 10:51 357阅读 0赞

这几天学习java,试着做文件资源管理器,整理了一些文件的操作。

  1. import java.io.File;
  2. import java.io.*;
  3. import java.io.IOException;
  4. public class OperateFile {
  5. //创建文件
  6. public static boolean createFile(String destFileName) {
  7. File file = new File(destFileName);
  8. if(file.exists()) {
  9. System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
  10. return false;
  11. }
  12. if (destFileName.endsWith(File.separator)) {
  13. System.out.println("创建单个文件" + destFileName + "失败,目标文件不能为目录!");
  14. return false;
  15. }
  16. //判断目标文件所在的目录是否存在
  17. if(!file.getParentFile().exists()) {
  18. //如果目标文件所在的目录不存在,则创建父目录
  19. System.out.println("目标文件所在目录不存在,准备创建它!");
  20. if(!file.getParentFile().mkdirs()) {
  21. System.out.println("创建目标文件所在目录失败!");
  22. return false;
  23. }
  24. }
  25. //创建目标文件
  26. try {
  27. if (file.createNewFile()) {
  28. System.out.println("创建单个文件" + destFileName + "成功!");
  29. return true;
  30. } else {
  31. System.out.println("创建单个文件" + destFileName + "失败!");
  32. return false;
  33. }
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. System.out.println("创建单个文件" + destFileName + "失败!" + e.getMessage());
  37. return false;
  38. }
  39. }
  40. //创建文件夹
  41. public static boolean createDir(String destDirName) {
  42. File dir = new File(destDirName);
  43. if (dir.exists()) {
  44. System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");
  45. return false;
  46. }
  47. if (!destDirName.endsWith(File.separator)) {
  48. destDirName = destDirName + File.separator;
  49. }
  50. //创建目录
  51. if (dir.mkdirs()) {
  52. System.out.println("创建目录" + destDirName + "成功!");
  53. return true;
  54. } else {
  55. System.out.println("创建目录" + destDirName + "失败!");
  56. return false;
  57. }
  58. }
  59. //创建临时文件
  60. public static String createTempFile(String prefix, String suffix, String dirName) {
  61. File tempFile = null;
  62. if (dirName == null) {
  63. try{
  64. //在默认文件夹下创建临时文件
  65. tempFile = File.createTempFile(prefix, suffix);
  66. //返回临时文件的路径
  67. return tempFile.getCanonicalPath();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. System.out.println("创建临时文件失败!" + e.getMessage());
  71. return null;
  72. }
  73. } else {
  74. File dir = new File(dirName);
  75. //如果临时文件所在目录不存在,首先创建
  76. if (!dir.exists()) {
  77. if (!CreateFile.createDir(dirName)) {
  78. System.out.println("创建临时文件失败,不能创建临时文件所在的目录!");
  79. return null;
  80. }
  81. }
  82. try {
  83. //在指定目录下创建临时文件
  84. tempFile = File.createTempFile(prefix, suffix, dir);
  85. return tempFile.getCanonicalPath();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. System.out.println("创建临时文件失败!" + e.getMessage());
  89. return null;
  90. }
  91. }
  92. }
  93. //删除文件
  94. public static boolean deleteDir(File dir){
  95. boolean success = true;
  96. if(dir.isDirectory()){
  97. String[] children = dir.list();
  98. for(int i = 0; i< children.length; i++){
  99. success = deleteDir(new File(dir, children[i]));
  100. if(!success){
  101. return false;
  102. }
  103. }
  104. success = dir.delete();
  105. }else{
  106. success = dir.delete();
  107. }
  108. return success;
  109. }
  110. //复制文件
  111. public static void copyFile(File sourceFile,File targetFile) throws IOException{
  112. // 新建文件输入流并对它进行缓冲
  113. FileInputStream input = new FileInputStream(sourceFile);
  114. BufferedInputStream inBuff = new BufferedInputStream(input);
  115. // 新建文件输出流并对它进行缓冲
  116. FileOutputStream output = new FileOutputStream(targetFile);
  117. BufferedOutputStream outBuff = new BufferedOutputStream(output);
  118. // 缓冲数组
  119. byte[] b = new byte[1024 * 5];
  120. int len;
  121. while ((len =inBuff.read(b)) != -1) {
  122. outBuff.write(b, 0, len);
  123. }
  124. // 刷新此缓冲的输出流
  125. outBuff.flush();
  126. //关闭流
  127. inBuff.close();
  128. outBuff.close();
  129. output.close();
  130. input.close();
  131. }
  132. // 复制文件夹
  133. public static void copyDirectiory(String sourceDir, String targetDir) throws IOException {
  134. // 新建目标目录
  135. (new File(targetDir)).mkdirs();
  136. // 获取源文件夹当前下的文件或目录
  137. File[] file = (new File(sourceDir)).listFiles();
  138. for (int i = 0; i < file.length; i++) {
  139. if (file[i].isFile()) {
  140. // 源文件
  141. File sourceFile = file[i];
  142. // 目标文件
  143. File targetFile = new
  144. File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
  145. copyFile(sourceFile, targetFile);
  146. }
  147. if (file[i].isDirectory()) {
  148. // 准备复制的源文件夹
  149. String dir1 = sourceDir + "/" + file[i].getName();
  150. // 准备复制的目标文件夹
  151. String dir2 = targetDir + "/" + file[i].getName();
  152. copyDirectiory(dir1, dir2);
  153. }
  154. }
  155. }
  156. //文件重命名
  157. public static void renameFile(String path,String oldname,String newname){
  158. if(!oldname.equals(newname)){//新的文件名和以前文件名不同时,才有必要进行重命名
  159. File oldfile=new File(path+"/"+oldname);
  160. File newfile=new File(path+"/"+newname);
  161. if(!oldfile.exists()){
  162. return;//重命名文件不存在
  163. }
  164. if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同,则不允许重命名
  165. System.out.println(newname+"已经存在!");
  166. else{
  167. oldfile.renameTo(newfile);
  168. }
  169. }else{
  170. System.out.println("新文件名和旧文件名相同...");
  171. }
  172. }
  173. //剪切文件
  174. public static void changeDirectory(String filename,String oldpath,String newpath,boolean cover){
  175. if(!oldpath.equals(newpath)) {
  176. File oldfile = new File(oldpath+"/"+filename);
  177. File newfile = new File(newpath+"/"+filename);
  178. if(newfile.exists()){//若在待转移目录下,已经存在待转移文件
  179. if(cover)//覆盖
  180. oldfile.renameTo(newfile);
  181. else
  182. System.out.println("在新目录下已经存在:"+filename);
  183. }
  184. else{
  185. oldfile.renameTo(newfile);
  186. }
  187. }
  188. }
  189. public static void main(String[] args) throws IOException {
  190. //----------------------复制文件---------------------//
  191. // 源文件夹
  192. String url1 = "f:/css";
  193. // 目标文件夹
  194. String url2 = "d:/tempPhotos";
  195. // 创建目标文件夹
  196. (new File(url2)).mkdirs();
  197. // 获取源文件夹当前下的文件或目录
  198. File[] file = (new File(url1)).listFiles();
  199. for (int i = 0; i < file.length; i++) {
  200. if (file[i].isFile()) {
  201. // 复制文件
  202. copyFile(file[i], new File(url2 +"\\"+ file[i].getName()));
  203. }
  204. if (file[i].isDirectory()) {
  205. // 复制目录
  206. String sourceDir = url1 + File.separator + file[i].getName();
  207. String targetDir = url2 + File.separator + file[i].getName();
  208. copyDirectiory(sourceDir, targetDir);
  209. }
  210. }
  211. }
  212. }

发表评论

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

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

相关阅读