• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Sean Keane
Ranch Hand
Posts: 590
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!