Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.canWrite is not working as per expectations

Tags:

java

java-io

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();
  }
like image 902
kunal dexit Avatar asked Dec 05 '25 09:12

kunal dexit


2 Answers

Have you tried using the java.nio.file.Files#isWritable method. as well as File#canWrite ?

like image 186
racc Avatar answered Dec 07 '25 22:12

racc


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.

like image 38
Chandra Sekhar Avatar answered Dec 07 '25 23:12

Chandra Sekhar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!