I need to check permissions to create files for a specific directory.
I tried:
try { AccessController.checkPermission(new FilePermission("directory_path", "write"));
... but this always throws a SecurityException (as I understand it, this does not check the basic permissions of fs, but some JVM parameters that must be explicitly configured).
Another possible way was to use something like this:
File f = new File("directory_path"); if(f.canWrite()) {
... but this returns true even if I cannot create the file in the specified directory (for example, I cannot create the file in "c: \" when starting my application as a user without administrator rights, but f.canWrite () returns true )
In the end, I did a hack similar to this:
File f = new File("directory_path"); try { File.createTempFile("check", null, f).delete(); // Have permission } catch (IOException e) { // Doesn't have permission }
... but this can only serve as a temporary solution, since I need to get such permissions for almost all folders on client fs.
Does anyone know how to properly obtain permissions to create REAL files without causing performance issues and hacks described above?
source share