Failed to delete file in Windows using Java

I am trying to delete a file on a Windows operating system using the Java IO API file.delete() . However, it fails and returns false. The same code works like a charm in Ubuntu.

I checked that the permissions of the file allow the program to delete it. Also, all input and output streams for the file were opened as an attempt with resources.

try (InputStream in = new FileInputStream(localFile); OutputStream out = new FileOutputStream(destinationFileName))

Using the debugger, I tested and found out that in the line of code I delete the file, it returns true for the following API calls.

 file.exists() file.canRead(); file.canWrite(); file.canExecute(); 

I even tried adding System.gc() right before the delete call to make sure all threads are closed.

Not sure if this is useful information, but I even tried using the Apocal commons FileUtils.forceDelete(file) method, and it also failed.

So what am I missing here?

Update:

Using Files.delete(Paths.get(file.getAbsolutePath())) , I got the following error.

 java.nio.file.FileSystemException: C:\Users\thuvvareka\Desktop\temp\in\sd.xml: The process cannot access the file because it is being used by another process. at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269) at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103) at java.nio.file.Files.delete(Files.java:1126) at org.adroitlogic.x.transport.file.FileMessageInjector.finalizeProcessing(FileMessageInjector.java:161) at org.adroitlogic.x.transport.file.FileMessageInjector.afterProcess(FileMessageInjector.java:123) at org.adroitlogic.x.transport.file.FileMessageInjector.afterProcess(FileMessageInjector.java:37) at org.adroitlogic.x.base.trp.ScheduledMessageInjector.lambda$2(ScheduledMessageInjector.java:72) at org.adroitlogic.x.api.trp.MessageReceiver.lambda$receive$3(MessageReceiver.java:100) at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:760) at java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:736) at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474) at java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:1962) at org.adroitlogic.x.core.MessageContext.lambda$createNewResponseFuture$2(MessageContext.java:459) at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:760) at java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:736) at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474) at java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:1962) at org.adroitlogic.x.core.MessageContext.completeMessageFlowSuccessfully(MessageContext.java:332) at org.adroitlogic.x.base.connector.EgressConnectorElement.sendMessage(EgressConnectorElement.java:185) at org.adroitlogic.x.base.connector.EgressConnectorElement.process(EgressConnectorElement.java:146) at org.adroitlogic.x.base.processor.AbstractProcessingElement.processMessage(AbstractProcessingElement.java:103) at org.adroitlogic.x.base.processor.TraceableProcessingElement.processMessage(TraceableProcessingElement.java:53) at org.adroitlogic.x.base.connector.IngressConnectorElement.receiveMessage(IngressConnectorElement.java:119) at org.adroitlogic.x.core.IntegrationPlatform.lambda$receive$0(IntegrationPlatform.java:81) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) 
+7
source share
4 answers

Use Files.delete(filePath) instead of file.delete() , since file.delete() has some window resolution problems.

+1
source

Welcome to windows.

 java.nio.file.FileSystemException: C:\Users\thuvvareka\Desktop\temp\in\sd.xml: The process cannot access the file because it is being used by another process. 

Usually, when a process has an open file in Windows, the operating system locks the file so that the file cannot be deleted. If this is your program in which the file is open when you try to delete it, first close the file and then delete it. If this is another program with an open file, you need to find out who opened it and from there.

When a process has a file open on Linux, usually nothing prevents you from deleting it, so you see a different behavior.

+1
source

Perhaps you can use System.Runtime.exec () to run a terminal / command line command to delete a specific file. It may be somehow platform dependent, but the command that needs to be entered in the exec () function may differ among the os properties.

You can check this thread to determine the current version of the java executable.

How can I programmatically determine the operating system in Java?

On linux, your line will look like this:

 System.Runtime.exec("rm <path to file>"); 
0
source

I came across this recently. I created a workaround in which if file.delete() returns false, I check if file.delete() returns true, and if so, I'll wait a bit, then try file.exists() again after a number of attempts .

My unproven suspicion is that the virus scanners in Windows block the file to scan the file, and waiting allows you to complete the virus scan.

  // Remove the original file. if(!file.delete()) { // wait a bit then retry on Windows if (file.exists()) { for (int i = 0; i < 6; i++) { Thread.sleep(500); System.gc(); if (file.delete()) break; } 
0
source

Source: https://habr.com/ru/post/1012504/


All Articles