无法理解文件对象的工作。

时间:2021-07-30 20:22:55

I am trying to delete a jar file using java 5 (So the Paths API introduced in Java 7 is not an option).

我尝试使用java 5删除一个jar文件(因此java 7中引入的路径API不是一个选项)。

My code:

我的代码:

String sep = File.separator;

File test = new File("."+ sep + "server" + sep + "lib" + sep + "testJar.jar");
if(test.delete())
{
    logger.log(Level.INFO,test.getName() + " deleted.");
}
else
{
    logger.log(Level.INFO,"Delete operation is failed for "+test.getName());
}

When my code gets executed the jar file is deleted but Delete operation is failed for testJar.jar is printed in the logs.Cant understand why..any help is appreciated.

当我的代码被执行时,jar文件被删除,但是testJar的删除操作失败。jar打印在日志中。不能理解为什么. .任何帮助都是感激。

UPDATE: I tried the same code again and and this time it says testJar.jar deleted Now i am confused what is happening

更新:我再次尝试了相同的代码,这次是testJar。jar删除了,现在我不知道发生了什么

2 个解决方案

#1


1  

I see this problem has magically disappeared, but some general troubleshooting tips:

我看到这个问题神奇地消失了,但是一些常见的故障排除技巧:

First of all; try creating and deleting another file and check file permissions/ownership.

首先;尝试创建和删除另一个文件并检查文件权限/所有权。

Check that another process isn't holding the file:

检查另一个进程没有保存文件:

$ lsof filename

Test the underlying OS call.

测试底层OS调用。

File.delete() will delegate to the underlying OS, and will typically end up calling remove (or unlink). If you use OpenJDK, you should be able to browse the source code. If not, see if you can trace what happens "under the covers".

delete()将委托给底层操作系统,通常最后会调用remove(或unlink)。如果您使用的是OpenJDK,那么您应该能够浏览源代码。如果没有,看看你能不能追踪“幕后”发生了什么。

Write a small snippet that simply executes this call and see how the OS behaves when you try and delete this file.

编写一个小代码段,该代码段只执行此调用,并在尝试删除该文件时查看操作系统的行为。

#2


0  

File delete fails if the file that you are trying to delete does not exists of is blocked. Also fails for security reasons (insufficient privileges to execute on local system)

如果要删除的文件不存在,则文件删除失败。也由于安全原因而失败(本地系统上执行的权限不足)

The tipical case is that is blocked for example if the jar is used by a running application, or opened exclusively, or analized by a tool (like an antivirus),...

典型的情况是,如果jar被运行的应用程序使用,或仅被打开,或被工具(如杀毒软件)进行了analize,则会被阻塞。

#1


1  

I see this problem has magically disappeared, but some general troubleshooting tips:

我看到这个问题神奇地消失了,但是一些常见的故障排除技巧:

First of all; try creating and deleting another file and check file permissions/ownership.

首先;尝试创建和删除另一个文件并检查文件权限/所有权。

Check that another process isn't holding the file:

检查另一个进程没有保存文件:

$ lsof filename

Test the underlying OS call.

测试底层OS调用。

File.delete() will delegate to the underlying OS, and will typically end up calling remove (or unlink). If you use OpenJDK, you should be able to browse the source code. If not, see if you can trace what happens "under the covers".

delete()将委托给底层操作系统,通常最后会调用remove(或unlink)。如果您使用的是OpenJDK,那么您应该能够浏览源代码。如果没有,看看你能不能追踪“幕后”发生了什么。

Write a small snippet that simply executes this call and see how the OS behaves when you try and delete this file.

编写一个小代码段,该代码段只执行此调用,并在尝试删除该文件时查看操作系统的行为。

#2


0  

File delete fails if the file that you are trying to delete does not exists of is blocked. Also fails for security reasons (insufficient privileges to execute on local system)

如果要删除的文件不存在,则文件删除失败。也由于安全原因而失败(本地系统上执行的权限不足)

The tipical case is that is blocked for example if the jar is used by a running application, or opened exclusively, or analized by a tool (like an antivirus),...

典型的情况是,如果jar被运行的应用程序使用,或仅被打开,或被工具(如杀毒软件)进行了analize,则会被阻塞。