• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

File.canWrite  RSS feed

Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working in windows environment. I have a folder on which i have set read only permissions. However, when I try to set read only permissions I'm given two options as to make only the particular folder as read only or make all the files and folders in the selected folder also as read only. I select the first option and make only the selected folder as read only.
The above explained actions are done in windows directly.
Now i write a java program to read that creates a File class using the above mentioned folder(permissions set to read only). However when I use the method canWrite(), the method returns true.
Can you explain why?

My purpose is, the user specifies a file/folder location. If it is a file I have to get the parent folder, check the permissions and delete only if the folder has write permissions and Give a warning if the containing folder is read only. But while testing i found the problem explained in the first paragraph.
Please suggest an alternative if possible..
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nirmal,
You can make use of the APIs in File class to accomplish what you need. Just go through the APIs : getParent(),isDirectory(),canWrite() methods. All you have to do is make use of these methods and build your logic.

Give it a try. Go through the javaDoc. It is very important. It is your guide to Java!

-- Abdel Olakara
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the above answer is not answering the original poster's question.

I think the answer to the original question is that Java is telling the truth. Try this experiment: -

  • Create a new folder using Windows Explorer
  • Set the new folder to Read-Only, using File Properties
  • Navigate to the new folder and create a new file in it

  • Sane people would expect that it would not be possible to create a new file in a read-only directory. But on Windows, it is possible - the read-only status of a directory seems to be ignored. Therefore, Java will tell you that you canWrite() a read-only directory, for the simple reason that you can!

    <TROLL feed="no">Mr Jobs will sell you a better computer, whenever you're ready...</TROLL>
    We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
    RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!