gpt4 book ai didi

java - windows环境下删除文件失败

转载 作者:行者123 更新时间:2023-12-02 04:26:34 25 4
gpt4 key购买 nike

我在windows环境下工作。

在我的 tearDown() 方法中,deleteSucceeded 返回 false。

尽管文件位于所需位置,但它永远不会被删除。

奇怪的是:

在unix环境下,同样的代码可以正常工作。

参见下面的类(class):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/ServiceMockTest-context.xml" })
public class ServiceMockTest {

private File actualSerializedObjectFile;

@Before
public void setUp() throws InvalidDocumentException,
SAXException, IOException,
ParserConfigurationException, InvalidGpecConfigurationException {
actualSerializedObjectFile = new File(OUTPUT_DIR,
"ServiceMockTest-" + name.getMethodName() + ".json");
System.out.println("actualSerializedObjectFile: "
+ actualSerializedObjectFile.getAbsolutePath());
}


@After
public void tearDown() {
boolean deleteSucceeded = actualSerializedObjectFile.delete();
if (!deleteSucceeded) {
throw new RuntimeException("Failed to delete temporary
output file for List<Map<String,Object>> serialized
object: " + actualSerializedObjectFile.getAbsolutePath());
}
}

}

更新:

我分析了这个错误,发现问题出在其他地方。

当我在 Debug模式下运行测试时,控件转到以下代码块:

/**
* Constructs a InvocationTargetException with a target exception.
*
* @param target the target exception
*/
public InvocationTargetException(Throwable target) {
super((Throwable)null); // Disallow initCause
this.target = target;
}

这发生在调用 delete() 方法之后。

注意:

我的文件未在其他任何地方打开,因此 delete() 调用不会像 Windows 环境中那样删除它。

错误日志:

java.lang.RuntimeException: Failed to delete temporary output file for List<Map<String,Object>> serialized object: E:\git\userapp\services\serializedObjects\ServiceMockTest-getSchedule.json
at com.userapp.services.ServiceMockTest.tearDown(ServiceMockTest.java:198)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:47)
at org.junit.rules.RunRules.evaluate(RunRules.java:18)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

最佳答案

Windows 不会让您删除应用程序仍打开的文件,但 Unix 会。这可能就是导致行为差异的原因。

关于java - windows环境下删除文件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32066989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com