File and Directory Permissions in JDK1.6

text zoom

In java 1.6 sun has introduced file handling API enhancement by which user can now set the readable, writable, and executable flag of a file in the local file system. This functionality has been added to the java.io.File class. The following are the methods added in the class.










  • public boolean setReadable(boolean readable, boolean ownerOnly)

  • public boolean setReadable(boolean readable)

  • public boolean setWritable(boolean writable, boolean ownerOnly)

  • public boolean setWritable(boolean writable)

  • public boolean setExecutable(boolean executable, boolean ownerOnly)

  • public boolean setExecutable(boolean executable)



 



Example Code:-



package visualbuilder;


import java.io.File;


public class Permission {



/**

* @param args

*/


public static void main(String[] args) {

    File file = new File("C:/a.txt");

    file.setExecutable(true);

    file.setReadable(true);

    file.setWritable(true);

    System.out.println("The Read permission "+ file.canRead());

    System.out.println("The Execute permission "+file.canExecute());

    System.out.println("The Write permission "+file.canWrite());

}


}



 



Output:- The following is the output of the above program.



The Read permission true

The Execute permission true

The Write permission true

                    

Copyright © 2012 VisualBuilder. All rights reserved