这是一个测试代码,试图实现一个特性。这个特性是将一个文件移动到一个新的目录,如果它大于一定的大小,以避免生成太大的文件。然而,我每次在文件上使用.move()方法时都会遇到这个错误:
线程“main”java.nio.文件中出现异常。文件系统异常:C:\用户\公共\操作功能块\src\obf_reg. docx-〉C:\用户\公共\操作功能块\src\完整文件夹\obf.reg[完整].docx:
该进程无法访问该文件,因为另一个进程正在使用该文件
如果您在运行时遇到异常,请使用以下方法进行处理:在运行时,如果您遇到异常,请使用以下方法进行处理:
下面是测试器文件(我使用的是apache POI和IntelliJ):
package com.example.demo;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.xwpf.usermodel.*;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
public class Tester {
public static void main(String[] args) throws IOException {
ZipSecureFile.setMinInflateRatio(0.001);
File pathn1 = new File("C:\\Users\\Public\\Operational_Functional_Block\\src\\obf_reg.docx");
if (!(new File(String.valueOf(pathn1)).exists())) {
XWPFDocument docx12 = new XWPFDocument();
XWPFParagraph par1 = docx12.createParagraph();
XWPFRun run1 = par1.createRun();
run1.setText("...---...");
run1.addBreak();
try (FileOutputStream out1 = new FileOutputStream(String.valueOf(pathn1))) {
docx12.write(out1);
out1.close();
docx12.close();
}
}
String repeatCommand = "whatever";
while (!repeatCommand.equals("N")) {
Path path = Paths.get(String.valueOf(pathn1));
if (Files.size(path) < 2350) {
System.out.println("size" + "------>" + Files.size(path));
} else {
File logM = new File("C:\\Users\\Public\\Operational_Functional_Block\\src\\FullFolders");
logM.mkdir();
// ------------------------------
File pathn2 = new File("C:\\Users\\Public\\Operational_Functional_Block\\src\\obf_reg.docx");
//File pathn3 = new File("C:\\Users\\Public\\Operational_Functional_Block\\src\\obf_reg[Full].docx");
//pathn2.renameTo(pathn3);
// ------------------------------
Path normPF = Paths.get(String.valueOf(pathn2));
Path logPF = Paths.get("C:\\Users\\Public\\Operational_Functional_Block\\src\\FullFolders\\obf.reg[Full].docx");
Files.move(normPF, logPF, REPLACE_EXISTING);
// ------------------------------
File file1 = new File("C:\\Users\\Public\\Operational_Functional_Block\\src\\obf_reg.docx");
file1.createNewFile();
// ------------------------------
XWPFDocument docx1 = new XWPFDocument();
XWPFParagraph par21 = docx1.createParagraph();
XWPFRun run21 = par21.createRun();
run21.setText("...---...");
run21.addBreak();
try (FileOutputStream out1 = new FileOutputStream(String.valueOf(pathn1))) {
docx1.write(out1);
out1.close();
docx1.close();
}
}
XWPFDocument docx = new XWPFDocument();
if (new File(String.valueOf(pathn1)).exists()) {
docx = new XWPFDocument(new FileInputStream(String.valueOf(pathn1)));
}
XWPFParagraph par2 = docx.createParagraph();
XWPFRun run2 = par2.createRun();
run2.setText("...---...");
run2.addBreak();
try (FileOutputStream out = new FileOutputStream(String.valueOf(pathn1))) {
docx.write(out);
}
System.out.println("did thing...");
}
}
}
1条答案
按热度按时间pw136qt21#
我应该用.copy而不是.move lol