Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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>
    please buy my thing and then I'll have more money:
    global solutions you can do in your home or backyard
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!