How can I check that I can delete a file in Java?
For example, I should be able to delete file C:/file.txt
but I never will be able to delete the C:/
or Computer
, or My Documents
etc.
Solution described in possible duplicate does not work for me.
Removing file requires write permission of the file's parent, i.e. directory where file is stored. Directory in java is also represented by instance of class java.io.File
that has method canWrite()
.
So, to check whether file can be deleted you should call file.getParent().canWrite()
.