File Class Enhancements : File « File « Java Tutorial






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








11.2.File
11.2.1.The File Class
11.2.2.Testing and Checking File Objects: file name and path
11.2.3.Providing a URI for a remote file
11.2.4.isAbsolute(): Returns true if the File object refers to an absolute pathname, and false otherwise.
11.2.5.getParent(): Returns the name of the parent directory of the file or directory represented
11.2.6.getParentFile(): Returns the parent directory as a File object, or null if this File object does not have a parent.
11.2.7.toString(): Returns a String representation of the current File object
11.2.8.exists(): Returns true if it exists and false otherwise
11.2.9.isDirectory(): Returns true if it is an existing directory and false otherwise
11.2.10.isFile(): Returns true if it is an existing file and false otherwise
11.2.11.isHidden(): Returns true if it is hidden and false otherwise
11.2.12.canRead(): Returns true if you are permitted to read the file and false otherwise.
11.2.13.canWrite(): Returns true if you are permitted to write to the file and false otherwise.
11.2.14.getAbsolutePath(): Returns the absolute path for the directory or file referenced by the current File object
11.2.15.getAbsoluteFile(): Returns a File object containing the absolute path for the directory or file referenced by the current File object.
11.2.16.list(): Returns a string array containing the children files and directories
11.2.17.listFiles(): Returns a File array containing the children files and directories
11.2.18.length(): Returns the length of current file in long
11.2.19.Create a human-readable file size
11.2.20.lastModified(): Returns last modified time in milliseconds since midnight on 1st January 1970 GMT
11.2.21.renameTo(File path): Rename a file or directory
11.2.22.setReadOnly(): Sets the file as read-only and returns true if the operation is successful
11.2.23.Filtering a File List
11.2.24.mkdir(): Creates a directory
11.2.25.mkdirs(): Creates a directory including any parent directories
11.2.26.createNewFile(): Creates a new empty file
11.2.27.createTempFile(String prefix, String suffix, File directory): a static method that creates a temporary file
11.2.28.delete(): delete the file or directory
11.2.29.Display File class constants and test some methods
11.2.30.deleteOnExit(): delete file or directory when the program ends
11.2.31.File Class Enhancements
11.2.32.Creates a file and sets it to read-only.
11.2.33.Create a file and change its attribute to readonly
11.2.34.List all roots
11.2.35.Get the free space
11.2.36.Get the usable space
11.2.37.Get the total space
11.2.38.Set file attributes.
11.2.39.Change a file attribute to writable
11.2.40.Change a file attribute to read only
11.2.41.Getting a Proper URL from a File Object
11.2.42.Get icon for file type
11.2.43.Get all xml files by file extension
11.2.44.Get file extension name
11.2.45.File.getCanonicalFile() converts a filename path to a unique canonical form suitable for comparisons.