This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pulling up File Permissions using Java API ???

 
Sarin Kuraganti
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I wanted to know whether Java provides any API to pull up each and every permission associated with a file.

For example: In Unix, a file has 3 sets of permissions as shown below:

<UserPermissions><GroupPermissions><Others'Permissions>

Example: -rwxrwxrwx

r - for read
w - for write
x - for execute

There are some methods provided in java.io.File, such as canRead() and canWrite(), which help in telling whether a file is readable or writable. But I did not find any API which tells whether a file is 'executable'. Also, I presume the methods above pull up the permissions pertaining to the owner of the file, but not for the group and others part of a Unix File's permissions.

Is there a way to pull up the read/write/executable permissions for all the 3 catergories namely, UserPermissions, GroupPermissions and Others'Permissions.

I appreciate your note on this and appreciate your time too.

regards,
sarin
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can do this without native code. It wouldn't be hard to do the JNI for it, though.

The canRead() and canWrite() methods, as far as I know, return a flag indicating whether the Java program can read or write the file. That does not map directly to any of the three sets of permissions in Unix.
 
Sarin Kuraganti
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Peter,

Thanks for the reply. Can you tell me exactly how to use JNI to invoke Unix Shell scripts? All I have seen is that JNI can be used to invoke C/C++ code, but could not find how to use JNI in conjunction with Shell scripts. Do you have any clue? Currently I have a script which pulls up the 'rwx' permissions of a file, I want to invoke this script from my java program to get things going. Can you suggest anything to fix this one?

BTW, nice pics on your website.

regards,
Sarin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic