1

I need to set the permission to read a file to a specific user of the operating system, how can I do this in Java?

Edit: The file will be created with permissions just to the user running the application, than it needs to set read permission to a single user, other users will not have the permissions to read the file.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Tiago Moraes
  • 318
  • 3
  • 7
  • 1
    Depends on the operating system. – Marcelo Jun 07 '11 at 17:07
  • windows and linux, does an universal API exist for this? – Tiago Moraes Jun 07 '11 at 17:08
  • You would basically have to execute the os specific command to change file permissions. You can look into ncmathsadist's answer and see if those methods are enough for what you pretend to do. – Marcelo Jun 07 '11 at 17:13
  • @Marcelo They are not. The file will be created with permissions just to the user running the application, than i need to set read permission to a single user, other users will not have the permissions to read the file. – Tiago Moraes Jun 07 '11 at 17:16
  • I couldn't find any library that does what you want. – Marcelo Jun 07 '11 at 17:19
  • See also http://stackoverflow.com/questions/664432/how-do-i-programmatically-change-file-permissions – Raedwald Aug 20 '13 at 14:53

4 Answers4

3

Use the methods setExecutable, setReadable, and setWritable in java.io.File. You can use these to change any permission bit of a file you own. Direct link: http://download.oracle.com/javase/6/docs/api/java/io/File.html#setReadable%28boolean%29. Testing this on MacOSX revels that only the user read value is changed. When program

import java.io.File;

public class Test
{
    public static void main(String[] args)
    {
        File f = new File("test.txt");
        f.setReadable(true);
    }
}

the folloiwng happens.

$ touch test.txt
$ chmod 000 test.txt


$ javac Test.java
$ java Test
$ ls -l test.txt
-r--------  1 morrison  staff  0 Jun  7 13:28 test.txt
ncmathsadist
  • 4,684
  • 3
  • 29
  • 45
  • This will give the permission just to a single user? Another user than the one running the application? The other SO users will have permission to read the file? – Tiago Moraes Jun 07 '11 at 17:10
  • 1
    This changes the file's read permission. A second argument can be use to restrict the change to the the owner only. Otherwise all permissions (chomod + r) change. – ncmathsadist Jun 07 '11 at 17:16
1

If you're targetting Windows Vista/7, build your JAR as EXE and embed a manifest requesting for Admin rights. If it's just an I/O problem, use the default File methods setReadable, setWritable, setExecutable :)

Vincent Koeman
  • 751
  • 3
  • 9
1

In regards to making the permissions for a single user, start with using ncmathsadist's code to add read permissions, then change the owner of the file to whoever needs access.

I found in the Ant source code they use for the change-owner task. For unix this can be found in the Ant source tree at org/apache/tools/ant/taskdefs/optional/unix/Chown.java. You might be able to include this and use it as an API call to change the user programmatically.

Matt D
  • 3,055
  • 1
  • 18
  • 17
0

if you want to change file permissions on old Java versions like Java 5, you can use this:

Runtime.getRuntime().exec("chmod 000 " + PATH + fileName);

on windows you'll have to replace chmod with the appropriate CACLS.exe command syntax