intellij-idea 如何防止在尝试移动文件时出现“该进程无法访问该文件,因为它正被另一个进程使用”错误?

jdg4fx2g  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(212)

这是一个测试代码,试图实现一个特性。这个特性是将一个文件移动到一个新的目录,如果它大于一定的大小,以避免生成太大的文件。然而,我每次在文件上使用.move()方法时都会遇到这个错误:
线程“main”java.nio.文件中出现异常。文件系统异常:C:\用户\公共\操作功能块\src\obf_reg. docx-〉C:\用户\公共\操作功能块\src\完整文件夹\obf.reg[完整].docx:
该进程无法访问该文件,因为另一个进程正在使用该文件
如果您在运行时遇到异常,请使用以下方法进行处理:在运行时,如果您遇到异常,请使用以下方法进行处理:
下面是测试器文件(我使用的是apache POI和IntelliJ):

  1. package com.example.demo;
  2. import java.io.*;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6. import org.apache.poi.openxml4j.util.ZipSecureFile;
  7. import org.apache.poi.xwpf.usermodel.*;
  8. import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  9. public class Tester {
  10. public static void main(String[] args) throws IOException {
  11. ZipSecureFile.setMinInflateRatio(0.001);
  12. File pathn1 = new File("C:\\Users\\Public\\Operational_Functional_Block\\src\\obf_reg.docx");
  13. if (!(new File(String.valueOf(pathn1)).exists())) {
  14. XWPFDocument docx12 = new XWPFDocument();
  15. XWPFParagraph par1 = docx12.createParagraph();
  16. XWPFRun run1 = par1.createRun();
  17. run1.setText("...---...");
  18. run1.addBreak();
  19. try (FileOutputStream out1 = new FileOutputStream(String.valueOf(pathn1))) {
  20. docx12.write(out1);
  21. out1.close();
  22. docx12.close();
  23. }
  24. }
  25. String repeatCommand = "whatever";
  26. while (!repeatCommand.equals("N")) {
  27. Path path = Paths.get(String.valueOf(pathn1));
  28. if (Files.size(path) < 2350) {
  29. System.out.println("size" + "------>" + Files.size(path));
  30. } else {
  31. File logM = new File("C:\\Users\\Public\\Operational_Functional_Block\\src\\FullFolders");
  32. logM.mkdir();
  33. // ------------------------------
  34. File pathn2 = new File("C:\\Users\\Public\\Operational_Functional_Block\\src\\obf_reg.docx");
  35. //File pathn3 = new File("C:\\Users\\Public\\Operational_Functional_Block\\src\\obf_reg[Full].docx");
  36. //pathn2.renameTo(pathn3);
  37. // ------------------------------
  38. Path normPF = Paths.get(String.valueOf(pathn2));
  39. Path logPF = Paths.get("C:\\Users\\Public\\Operational_Functional_Block\\src\\FullFolders\\obf.reg[Full].docx");
  40. Files.move(normPF, logPF, REPLACE_EXISTING);
  41. // ------------------------------
  42. File file1 = new File("C:\\Users\\Public\\Operational_Functional_Block\\src\\obf_reg.docx");
  43. file1.createNewFile();
  44. // ------------------------------
  45. XWPFDocument docx1 = new XWPFDocument();
  46. XWPFParagraph par21 = docx1.createParagraph();
  47. XWPFRun run21 = par21.createRun();
  48. run21.setText("...---...");
  49. run21.addBreak();
  50. try (FileOutputStream out1 = new FileOutputStream(String.valueOf(pathn1))) {
  51. docx1.write(out1);
  52. out1.close();
  53. docx1.close();
  54. }
  55. }
  56. XWPFDocument docx = new XWPFDocument();
  57. if (new File(String.valueOf(pathn1)).exists()) {
  58. docx = new XWPFDocument(new FileInputStream(String.valueOf(pathn1)));
  59. }
  60. XWPFParagraph par2 = docx.createParagraph();
  61. XWPFRun run2 = par2.createRun();
  62. run2.setText("...---...");
  63. run2.addBreak();
  64. try (FileOutputStream out = new FileOutputStream(String.valueOf(pathn1))) {
  65. docx.write(out);
  66. }
  67. System.out.println("did thing...");
  68. }
  69. }
  70. }
pw136qt2

pw136qt21#

我应该用.copy而不是.move lol

相关问题