Java's File.delete() sometimes leaves behind inaccessible files (Windows)
NickName:MellowCoder Ask DateTime:2016-07-08T21:25:16

Java's File.delete() sometimes leaves behind inaccessible files (Windows)

I am trying to programatically purge log files from a running(!) system consisting of several Java and non-Java servers. I used Java's File.delete() operation and it usually works fine. I am also perfectly fine with log files that are currently in use not being deleted, so I just log it as a warning whenever File.delete() returns false.

However, in log files which are currently still being written to by NON-Java applications (Postgres, Apache HTTPD etc., Java applications might also be affected, but I didn't notice yet, and all are using the same logging framework anyway, which seems to be OK) are not actually deleted (which is what I expected), however, File.delete() returns "true" for them. But not only do these files still exist on the file system (Windows explorer and "dir" still show them), but afterwards they are inaccessible... when I try to open them with a text editor etc. I get "access denied" or similar error messages, when I try to copy them with explorer, it also claims that I do not have permissions, when I check its "properties" with explorer, it gives me "You do not have permission to view or edit this object's permissions".

Just to be clear: before I ran the File.delete() operation, I could access or delete these files without any problems, the delete operation "breaks" them. Once I stop the application, the file then disappears, and on restart, the application creates it from scratch and everything is back to normal. The problem is that when NOT restarting the application after the log file purge operation, the application logs to nirvana.

This behavior reminds me a bit of the file deletion behavior of Linux: if you delete a file that is still held open by an application, it disappears from the file system, but the application - still holding a file handle - will happily continue writing to that file, but you will never be able to access it afterwards. The only difference being that here the files are still visible in the FS, but also not accessible otherwise.

I should mention that both my Java program and the applications themselves are running with "system" user.

I also tried Files.delete(), which allegedly throws an IOException indicating the error... but it seems there is no error.

What I tried to work around the problem is to check if the files are currently locked, using the method described here https://stackoverflow.com/a/1390669/5837050, but this only works for some of the files, not for all of them.

I basically need a reliable way (at least for Windows, if it worked also for Linux, that would be great) to determine if a file is still being used by some program, so I could just not delete it.

Any hints appreciated.

Copyright Notice:Content Author:「MellowCoder」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38267878/javas-file-delete-sometimes-leaves-behind-inaccessible-files-windows

More about “Java's File.delete() sometimes leaves behind inaccessible files (Windows)” related questions

Java Servlet and Java

What's the difference between Java Servlet and core Java? Is that difficult to learn Java Servlet assuming some background in Java programming?

Show Detail

Java Security Exception for Java AST

I am using eclipse 32bit in my office and developing a java code to parse JavaAST, but when I try to run the program at home I am getting a getting this exception. I am also providing where the Exc...

Show Detail

Java 11 - java/beans/PropertyChangeListener

Getting below with java11. This was working fine with java 8. Please suggest. This is happening only when running in eclipse 4.12 Is there a way we can add module java.desktop when compliance level...

Show Detail

Java 8 and Java time

I know the default Java library for Codename One is now Java 8 and I am also compiling locally with Java 8. I am trying to use the new java.time API added in Java 8 (Tutorial). I see the following

Show Detail

java.io.IOException - Java

I am getting repeatedly below error while running java application on IBM - WebSphere Application Server. [ 00000024 TCP E org.jgroups.protocols.TP$IncomingPacket run failed handling incoming mess...

Show Detail

Java , Android , Java EE

How diferent is Java used in Android apps development from Java used to develop regular desktop applications? Can I use the skills acquired by developing Java Android applications in Java EE web

Show Detail

Java meaning of .java~

By typing jar -tf myJar.jar MyProject I get in the file list, besides the normal .java and .class files, also a .java~ file. I have read googling it around that it is due to some other incomplete v...

Show Detail

Java: java.lang.NoClassDefFoundError

I'm sorry if this has been posted before but I seem to have read and understand that there are many possible causes to this problem and fixes. I am creating a Jetty server and am trying to compile it

Show Detail

Java starting another Java Application

I am in the process of building a wrapper jar for a jar that I built. It will handle updating the main application and making sure users are valid users. I am having a major issue though, because I...

Show Detail

Java NullPointerException in Java Agent

I'm developing a java agent. I have an NullPointerException error which I do believe should not happen. Here is the debug console message: java.lang.NullPointerException at sun.net.www.http.

Show Detail