0

In my program i want to create some temporary directories namely Temp1 and Temp2.

I am currently working on Mac and i choose the place to create these directories as

/Users/your_account_username/Library/Application Support

This directory has the following File Permissions

> drwx------+ 27 ib-mac-1 staff 918 May 7 19:01 Application Support 

When i create Temp1 , i set read, write, execute permissions using the Java File API.

file.setExecutable(true); file.setReadable(true); file.setWritable(true); 

Somehow this is not helping me apply rwx to the directories, the created directories/files under them are taking the only drwxr-xr-x

2 Answers 2

4

File.setExecutable(boolean), File.setReadable(boolean) and File.setWritable(boolean) set only the owner permissions.

As schmop says, there are variants where you can indicate that you want the permissions set for everyone (file.setReadable(true, false), etc).

However, if you want precise control of the POSIX file permissions (setting different other and group permissions, for example), the easiest solution is to use Java 7. Java 7 added the Path interface and Paths and Files classes as a replacement for File. It provides the method Files.setPosixFilePermissions, which can give you precisely the file permissions you want.

Sign up to request clarification or add additional context in comments.

1 Comment

In java 6, there are variants to set for all users, for instance setReadable(boolean readable, boolean ownerOnly)
2

Use

file.setExecutable(true, false); file.setReadable(true, false); file.setWritable(true, false); 

to set for all users. Available in Java 6 (at least).

2 Comments

i see that if i apply this to the parent directory, only the parent directory gets 777. is there a way to do this recursive ? so that all children under parent get 777 ?
@shridatt Not automatically. You will have to traverse the children with the listFiles() method in the File class. However, if you are only executing your program on OSX, cosider using the exec() family of methods in the Runtime class. It will allow you to do most things available in a terminal, including chmod.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.