Rajendra's Blog

Wednesday Apr 02, 2008

JSR-203 New File System APIs

JSR-203 Specification gives clear description of the new API's. It does not take much time to get acquainted with the new API's. I just want to give some simple fundamentals for you to get started with the new API's. You can have a look at this entry and play around with new feature without much effort.

The method which enables Java virtual machine to access the underlying file system objects is Path.get() mehthod.

             Path.get(“file”);

Path.get(String path) is a static method in java.nio.file package which returns a path object for the file given in a string. This path locates the file in the native (underlying) file system. Okay, after having got access to file what operations can we perform on this file: I will just list some of the interesting operations with code fragment examples.

>> Creating a symbolic link

Assume you have target folder “My Album” which is in home directory “/home/rajendra/”. Now if you want to create a symbolic link for this folder in shared NFS directory “/net/export/share/My Album” then you have to use Path#createSymbolicLink(Path target) method. The following code snippet creates a symbolic link.

   Path target = Path.get("/home/rajendra/My Album"); 
   Path symLink = Path.get("/net/export/share/My Album");
   symLink.createSymbolicLink(target);

The first line creates the path object for the folder "/home/rajendra/My Album". Second line is the symbolic link which you want to create for the target file. Finally third line creates the symbolic link.

If you are creating a link in Vista Operating System use this method to create a link. This method creates a right link depending on the target.

>> Reading a symbolic link and traversing a directory

Path.readSysmbolicLink() method returns a path object for the target file to which the link is pointed.

Lets create a symbolic link to a directory in Vista and try to read the target of the link.

C:\>mklink SymLnkToMyDocs C:\MyDocs
symbolic link created for SymLnkToMyDocs <<===>> C:\MyDocs

The following code fragment prints the target of symbolic link and tries to iterate the entries of target folder.

	Path path = Path.get("c:\\SymLnkToMyDocs");
        Path target = path.readSymbolicLink();
        System.out.println("target file: "+ target);
        DirectoryStream ds = path.newDirectoryStream((Filter)null);
        for (DirectoryEntry de : ds){
            System.out.println("file " + de.getFileName());
        }

Here S.O.P. prints the target file: C:\MyDocs. But path.newDirectoryStream() with null value as filter throws “java.nio.file.NotDirectoryException: c:\SymLnkToMyDoc”

This is due to we have created wrong symbolic for the directory. In Vista if we do not mention /D option it treats the target as a file. If we had created a symbolic link with the following option then executing above program would not throw any exception.

C:\> mklink /D SymLnkToMyDocs C:\MyDocs
That's it for today. We will explore other methods in the Path class in next entry.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed

Calendar

Feeds

Search

Links

Navigation

Referrers