Monday, February 13, 2012

New methods of the File class

In Java SE 6.0, the File class has been enhanced to include new methods that provide information about disk usage:

  • getTotalSpace()
  • getFreeSpace()
  • getUsableSpace()
getTotalSpace()
The getTotalSpace method provides information on a particular File's partition size in bytes.
getFreeSpace()
The getFreeSpace method provides information about the available space still unused in a particular File partition in bytes.
getUsableSpace()
Similar to getFreeSpace, the getUsableSpace method further checks for OS restrictions and available write permissions associated with the File.
This code is an example of how to use the getTotalSpace, getFreeSpace, and getUsableSpace methods.
import java.io.*;

public class SampleFileMethod {
  public static void main(String args[]) {

       if(args.length>0) {
    File f=new File(args[0]);
    System.out.println(" Total size of " + f + " is=:"+ f.getTotalSpace());
    System.out.println(" Available space of " + f + " is=:"+ f.getFreeSpace());
    System.out.println(" Usable space of " + f + " is=:"+ f.getUsableSpace());
      } else {
    System.out.println(" Enter a Filename");
    }
  }
}
Additional methods used to specify restrictions or permissions in reading, writing, or executing a File object are
  • setWritable()
  • setReadable()
  • setExecutable():
setWritable()
The setWritable method specifies the permissions on write operations to a File object. This method is overloaded as follows:
  setWritable(boolean writable)
  setWritable(boolean writable, boolean ownerOnly)

The first method is used to specify if the file is writable or not. The second method enhances the first by specifying if the write permission is applicable only to the owner.
setReadable()
The setReadable method specifies the permissions on read operations to a File object. This method is overloaded as follows:
  setReadable(boolean readable)
  setReadable(boolean readable, boolean ownerOnly)
The first method specifies if the file can be accessed or not. The second method enhances the first by specifying if the read permission is applicable only to the owner.

If the underlying file system cannot distinguish the owner's read permission from that of others, the permission will apply to everybody, regardless of this value.
setExecutable():
The setExecutable method specifies the execute permissions on a File object being executed. This method is overloaded as follows:
  setExecutable(boolean executable)
  setExecutable(boolean executable, boolean ownerOnly)


The method used to test if the file can be executed or not is the canExecute() method.
This code is an example of how setWritable() can be used.
import java.io.*;

public class SampleFileWrite {

  public static void main(String args[]) {
       if(args.length>0) {
    File f=new File(args[0]);
    f.setWritable(false);
    System.out.println("File writable? " + f.canWrite());
    //checks if the File is editable
    f.setWritable(true, true);
    System.out.println("File writable? " + f.canWrite());
    //checks if the File is editable
      } else {
    System.out.println(" Enter a Filename");
    }
  }
}
The canWrite method is used to check whether the file can be edited or is read only (where setWritable() is set to false).

1 comments:

Post a Comment