Masoud Kalali's Blog

My thoughts on software engineering and beyond…


Introducing NIO.2 (JSR 203) Part 4: Changing File System Attributes and Permissions

This is the 4th installment of my entries covering NIO.2. In this entry I will discuss more about what we can do with attributes and permissions.  The NIO.2 lets us write the permissions and attributes of a file in addition to reading them. For example we can change the rwx permissions using the Attributes utility class and  PosixFilePermission.

You can execute the following sample code with almost no changes, the only thing you need to change is the path to our sample file and then you are ready to see how it works.

Let’s see what we have done starting from top: at the beginning we have just initialized the FileSystem object and got a file reference to /home/masoud/dl. Then we read all of this files basic and posix attributes using the attributes view notation. The following sample code shows this step

Next step we have use the PosixPermissions class to create a permission set using the OS permissions presentation and then applied these permissions on our file

In the next step we created a user principal for a user named masoud and changed the ownership of our file to this user. We have done this using both available methods.

Finally we read the file permissions to see what has changed after we applied this new permissions on our file. using the following snippet.

The output of running the above sample code is shown below:

You may ask what is the attribute view notation and what is meaning of that posix:permissions or posix:*. The first one means that we want to retrieve the permissions property of the file while the second one means that we want to retrieve all attributes of the file which can be seen using the posix view.

Now you are asking where you can found list of this attributes view notations, then you question is very right. The following links shows what attributes are available to read using each one of those views. Each attribute is one of the properties included in the interface.

Sure we have other views, you can find them in the JavaDocs linked above.

Next entry will be the last entry covering File System functionalities of the NIO.2 including the watch and change notification service and the ACL list. After that we will look at new features included for IO developers. You can find other entries of this series under the NIO.2 tag of my weblog.

One Response to Introducing NIO.2 (JSR 203) Part 4: Changing File System Attributes and Permissions

  1. Pingback: Tweets that mention How to use NIO.2 (JSR-203) to update file permissions. | Masoud Kalali's Blog --

Leave a Reply

Your email address will not be published. Required fields are marked *

Answer the question if you are not a bot. *