Home > Back-end >  java.nio.file.FileSystemException: C:\p12\dummy.p12: The process cannot access the file because it
java.nio.file.FileSystemException: C:\p12\dummy.p12: The process cannot access the file because it

Time:11-18

I have a p12 file upload function with the following code:

-

Then I want to create a function to delete the p12 file with the following code:

-

And when I run the result there is an error:

java.nio.file.FileSystemException: C:\p12\dummy.p12: The process cannot access the file because it is being used by another process.

Is there a way for the file to be deleted successfully?

UPDATE : I've found the problem, apparently because the p12 file is used in this function:

-

Is there a way to still be able to delete the p12 file?

CodePudding user response:

The exception is telling you that there is another process that has the file open and therefore it can't be deleted. Take a look at your system processes (most probably applications) in Windows and see which application has the file open. Do you have the file open in a text editor such as notepad or in a command line shell? You have to close it there before it can be deleted.

You open the file

InputStream keyStoreStream = new FileInputStream(fileDir);

but the resource is never closed. Enclose the relevant parts in a try-with-resources block

Map<String, String> certSn;
try (InputStream keyStoreStream = new FileInputStream(fileDir)) {
    certSn =  = getP12Cert(keyStoreStream, passphrase.getPassphrase());
    // set up your assigneeModel here
} catch (IOException e) {
    // TODO throw or handle the exception however you need to
}

// rest of code here
  • Related