Managing the File System
Java 6 gives you much finer control over your local file system. For example, it is now easy to find out how much free space is left on your hard drive. The java.io.File
class has the following three new methods to determine the amount of space available (in bytes) on a given disk partition:
File homeDir = new File("/home/john");
System.out.println("Total space = " + homeDir.getTotalSpace());
System.out.println("Free space = " + homeDir.getFreeSpace());
System.out.println("Usable space = " + homeDir.getUsableSpace());
As their names would indicate, these methods return the total amount of disk space on the partition (getTotalSpace()), the amount of currently unallocated space (getFreeSpace()), and the amount of available space (getUsableSpace()), after taking into consideration OS-specific factors such as write permissions or other operating-system constraints. According to the documentation, getUsableSpace() is more accurate than getFreeSpace().
When I ran this code on my machine, it produced the following output:
Total space = 117050585088
Free space = 100983394304
Usuable space = 94941515776
File permissions are another area where Java 6 brings new enhancements. The java.io.File class now has a set of functions allowing you to set the readable, writable, and executable flags on files in your local file system, as you would with the Unix chmod command. For example, to set read-only access to a file, you could do the following:
File document = new File("document");
This will set read-only access for the owner of the file. You can also modify the access rights for all users by setting the second parameter (ownerOnly) to false:
documentsDir.setReadable (true, false);
documentsDir.setExecutable (false, false);
Naturally, this will work only if the underlying operating system supports this level of file permissions.
Just a Few of Many
Java 6 offers many other new features that I haven't mentioned here, such as support for JAX-WS Web services and JAXB 2.0 XML binding, improvements in the Swing and AWT APIs, and language enhancements such as sorted sets and maps with bidirectional navigation. Try it out!