3

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();
  }
kunal dexit
  • 97
  • 2
  • 9
  • 2
    You haven't said what you changed the permissions *to*... – Jon Skeet Mar 21 '12 at 14:11
  • you are doing something wrong in how you think you are setting permissions, your posted code should work as expected if the permissions are set correctly. –  Mar 21 '12 at 14:16
  • who is the owner of the file and who is running the application? – Woot4Moo Mar 21 '12 at 14:36
  • through eclipse i am trying to run the application. i myself created the file, but then took out the permission to write in it. all operations i am doing in my local desktop only. – kunal dexit Mar 21 '12 at 14:40

4 Answers4

3

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

racc
  • 1,222
  • 10
  • 13
  • I have had same issue with canWrite giving me incorrect answers when I asked if I can create files into the direction. But your answer has fixed the issue ;) – Martin Krajčírovič Aug 16 '18 at 03:23
  • That's helpful, thanks. `File.canWrite` considers `new File("C:/Windows/System32/");` to be writable, even for non-admin users. `Files#isWritable` actually checks if the current user is allowed to write. – Eric Duminil Nov 15 '22 at 12:59
2

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.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • i have changed the permissions to "deny" in windows OS. with the same file i am trying to log my entries. but while creating the logger with the same file, i am getting the exception saying java.io.FileNotFoundException: D:\myproject_log\delivery_report_edr.out (Access is denied) – kunal dexit Mar 21 '12 at 14:29
  • @user1282551 can you say how you are changing the permission? – Chandra Sekhar Mar 21 '12 at 14:31
  • in windows, right clicking on the file and then going in properties... there in security tab, i have changed the permission for write by clicking on "deny" option. is it the correct way??? – kunal dexit Mar 21 '12 at 14:34
  • @user1282551 sorry I can't help you more, but I can say if you change the **General property** of a file to **read-only** then you will get proper outputs. – Chandra Sekhar Mar 21 '12 at 14:51
  • 1
    The documentation says: "true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise". In many situations, canWrite returns true even though the application is _not_ allowed to write to the file, so I wouldn't call this confirming to the documentation at all. The documentation really should be clearer that it won't check permissions to the file at all. – Hakanai Feb 19 '13 at 23:18
0

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.

Marko
  • 20,385
  • 13
  • 48
  • 64
Bob
  • 1
0

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.

Ups
  • 13
  • 3