0

I have a problem with deleting the file after I wrote to it. Here is the code of writing to it:

String xml = "blablaxml";
Path file = Paths.get("file.xml");
Files.write(file, xml.getBytes());

Here I want to delete it:

Files.deleteIfExists(file);

The error:

java.nio.file.FileSystemException: file.xml: The process cannot access the file because it is being used by another process.

I debugged the code and just before trying to delete the file I used Microsoft Process Explorer and the file is not used by any process, so I don't understand the error.

BliXer
  • 11
  • 1

1 Answers1

-2

The error message is clear, you can't delete the file because it is used by another process or thread. Make sure you close the file stream after you finished writing into the file before you delete. See How to create a file and write to a file in Java?

Community
  • 1
  • 1
cdaiga
  • 4,318
  • 3
  • 17
  • 34