• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get File.canRead() to return false?

 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone know how I can get File.canRead() method to return false for a file that exists http://download.oracle.com/javase/1.5.0/docs/api/java/io/File.html#canRead()

I tried the following code, but it didn't work:

Thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The easiest way to get canRead() to return false is to use a file that is owned by another user -- and who didn't provide permission to the user running the java program.

Henry
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Any ideas if it is possible to change the permissions on the file using Java so that I can programmatically take a arbitary file and ensure that the canRead() method will fail? From a quick Google it seems this is not possible in Java 5.

So it sounds like the only way I can get canRead() method to fail is by manually setting the permissions on the file in the O/S and then running my Java program? Sound right?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Keane wrote:So it sounds like the only way I can get canRead() method to fail is by manually setting the permissions on the file in the O/S and then running my Java program? Sound right?



Yes, that sounds right. But so what? Surely having canRead() return false can't be a real-life requirement which needs to be automated?
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason I was interested in this is because I use the canRead() method as a precondition in one of my own methods in my API. I was looking to unit test this precondition failing.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Unfortunately, as you probably figured out, you need Java 6 to change permissions on files.

For Java 5, you need to get platform specific... example, for Unix, you need to exec the chmod command.

Henry
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, found out some info about file permissions, this page here has some good info http://stackoverflow.com/questions/664432/java-how-do-i-programmatically-change-file-permissions

I don't think I'll bother with native commands, so will have to put up with a little less than 100% test coverage :p

Thanks for the replies folks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic