Mega Code Archive

 
Categories / Java Tutorial / File
 

File Class Enhancements

The java.io.File class in Mustang comes with several new features. The java.io.File class in Mustang provides methods for checking hard disk space and the amount used. The java.io.File class in Mustang possesses methods for changing file attributes. Here are the new methods: public long getFreeSpace ()   Returns the size, in bytes, of the partition referenced by this File object. public long getFreeSpace ()   Returns the amount of free space, in bytes, in the partition referenced by this File object. public long getUsableSpace ()   Returns the number of bytes available to this virtual machine on the partition referenced by this File object. The difference between getUsableSpace and getFreeSpace is that the former takes into account restrictions imposed by the operating system, such as write permissions. The latter does not. public boolean setWritable (boolean writable, boolean ownerOnly)   Sets the owner's or everybody's write permission for the path referenced by this File object. public boolean setWritable (boolean writable)   Sets the owner's write permission for the path referenced by this File object. public boolean setReadable (boolean readable, boolean ownerOnly)    Sets the owner's or everybody's read permission for the path referenced by this File object. public boolean setReadable (boolean readable)    Sets the owner's read permission for the path referenced by this File object. public boolean setExecutable (boolean executable, boolean ownerOnly)    Sets the owner's or everybody's execute permission for the path referenced by this File object. public boolean setExecutable (boolean executable)   Sets the owner's execute permission for the path referenced by this File object. public boolean canExecute ()    Tests if the application has the right to execute the file referenced by this File object. Java 6 adds canExecute plus setReadable, setWritable, and setExecutable methods to change a file's attributes. import java.io.File; public class DiskSpaceDemo {   public static void main(String[] args) {     File file = new File("C:");     long totalSpace = file.getTotalSpace();     System.out.println("Total space on " + file + " = " + totalSpace + "bytes");     // Check the free space in C:     long freeSpace = file.getFreeSpace();     System.out.println("Free space on " + file + " = " + freeSpace + "bytes");   } } Total space on C: = 40015953920bytes Free space on C: = 6470483968bytes