RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Mustang Must-Haves: What's Cool in Java SE 6 : Page 4

The just-released Java SE 6 boasts many neat new features, such as integrated scripting, enhanced JDBC features, better Web service support, and much more. Here are few personal favorites from a seasoned Java developer and architect.

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");
	documentsDir.setReadable (true);
	documentsDir.setExecutable (false);

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.setWritable(false, 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!

John Ferguson Smart is principal consultant at Wakaleo Consulting, a company that provides consulting, training, and mentoring services in Enterprise Java and Agile Development. Well known in the Java community for his many published articles and talks, he is also the author of the book Java Power Tools.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date