Reputation: 107
i am trying to check whether a file is writable or not. i have changed the file permission by myself for all users. but if i try to run the program, it show "true" as a response. if i allow the permissions, then also it is showing "true".
Whats is my problem?
try
{
File file = new File("D:/myproject_log/delivery_report_edr.out");
if(!file.canWrite())
{
System.out.println("you can't write!!!");
}
else
System.out.println("you can write!!!");
}
catch(Exception e)
{
e.printStackTrace();
}
Upvotes: 3
Views: 6889
Reputation: 13
I had the same issue with a file located in c:\programFiles\folder and the File.canWrite method returned true and i was getting the same exception. when i changed the permission of write as allowed true for USER defined in security tab of Folder properties, It gave me no exception.
Upvotes: 0
Reputation: 1282
Have you tried using the java.nio.file.Files#isWritable method. as well as File#canWrite ?
Upvotes: 3
Reputation: 1
I have also found that File.canWrite()
cannot be trusted, especially over network drives, often returning true
even though a file write will fail or vice-versa. I made my own method that actually tries to write a dummy file to the dir. That is simple to write and foolproof. Maybe they fixed it though.
Upvotes: 0
Reputation: 19500
It is working fine. I copied your code and run it twice. First I got You can write, then right click on the file folder, go to properties and select read only and run the program again and I got you can't write
So as per the documentation of the method canWrite() it gave me the expected output. Please confirm your settings once again and check.
Upvotes: 2