DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Pulling up File Permissions using Java API ???

  1. #1
    Join Date
    Feb 2005
    Posts
    11

    Exclamation Pulling up File Permissions using Java API ???

    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,
    sbk

  2. #2
    Join Date
    Jan 2005
    Posts
    45
    Hello,

    Simply check out the FilePermission class .

    Hope that helps,
    Lionel Badiou
    CodeFutures -
    Java Code Generation

    http://www.codefutures.com



  3. #3
    Join Date
    Feb 2005
    Posts
    11

    Unhappy

    Hi Lionel,

    Thanks for the reply. But how actually can we use the java.io.FilePermissions class? This class does not provide any constructor which accepts a java.io.File so that I can read the permissions associated with that file.

    I am a bit confused, can you explain how we can use this class to retrieve the file permissions for the three categories, i.e User, Group and Others?

    Thanks,
    sbk

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If you have the File object you should be able to pull
    out the info you need for the FilePermission constructor's
    parameters.
    eschew obfuscation

  5. #5
    Join Date
    Feb 2005
    Posts
    11

    Unhappy

    Hi sjalle,

    Thanks for the comment. But I am confused on using the FilePermission constructor to pull up the permissions I need. The only constructor that FilePermission provides is:

    FilePermission(String path, String actions)

    which, creates a new FilePermission object with the specified actions.

    Can you explain how you used this object?

    thanks,
    sbk

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Ehh, my unix is a bit rusty but I'm willing to eat my own
    hat
    (and I don't have any) if java has no method of supplying
    this information programmatically. Unfortunately I have no way of
    checking that on my win2000...
    eschew obfuscation

  7. #7
    Join Date
    Feb 2005
    Posts
    11

    Unhappy

    Hi Sjalle,

    I am still not sure how Java provides the feature for checking the file permissions of a unix file. I tried using java.io.FilePermission class, but it allows only one set of Read,Write and Execute Permissions just like a Windows file, but not 3 sets of Permissions like a Unix File. I am curious how java provides this feature. I think one way of doing it is to use JNI. Have to see. Any comments???

    Thanks and regards,
    Sarin

  8. #8
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Off my head...

    This may be totally hahaha, but:

    You could use a Runtime to issue the unix command that displays the file
    permissions, and catch the output from that runtime.

    Here is an example were I issue a dos find command:

    Code:
      String [] cmd=new String [] {
          "c:\\WINNT\\system32\\find",
          "\"hello\"",
          "c:\\tmp\\words.txt"
        };
        Runtime rt=Runtime.getRuntime();
        Process pr=rt.exec(cmd);
        System.out.println("OK");
        InputStream in=pr.getInputStream();
        InputStreamReader ir=new InputStreamReader(in);
        BufferedReader br=new BufferedReader(ir);
        String line=null;
        while( (line=br.readLine()) != null) {
          System.out.println(line);
        }
    eschew obfuscation

  9. #9
    Join Date
    Feb 2005
    Posts
    11
    Thanks sjalle,

    Actually, I started using the Runtime class to execute a script yesterday, but then, I could not retrieve the return value from the script. Your code was very helpful to retrieve the return value from the script today. Thanks, it worked.

    regards,
    sbk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center