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


Utility Class Fills in Java's Missing Functionality : Page 3

Anybody who regularly switches between Perl and Java can find Java lacking in elementary operations. Try out a utility that bundles all the methods and helper classes that Java is missing into one class.

Directory Copy
Two methods, copy(File from, File to) and copy(String form, String to), copy either files or directories, depending on the type of File from. Two other methods, copy(File from, File to, String what) and copy(String from, String to, String what), help copy a file or a subdirectory (what) from a directory (from) to a directory (to). The Java.net forum posted a cool suggestion for this operation.

Several other methods help deal with the results of copying and/or help decide what to copy where. Boolean equal(File left, File right) returns true if the two files are equal (same contents, same timestamps) or, in case of directories, the entire directory trees are identical. Synchronize(File left, File right) and synchronize(File left, File right, String what) help eliminate the difference; they copy missing files over and replace older files with newer versions, in both directions.

Scanning Directory Trees: Inversion of Control
Scanning a directory tree and doing something with the contents of each node is a common task. Schools taught that recursion kicks in there. If you open a zip file and start browsing though its contents, you find no tree there, just files with compound names. Why not do the same with ordinary directories? Must you create separate recursive methods for every operation you do on directories? Of course not. Simply move all tree browsing into an iterable. If necessary, choose preorder or postorder traversal.

For width-first traversal, you need an additional queue—but who traverses a directory tree horizontally? It is totally unnatural. The natural way is depth first, which is why you need recursion. With preorder, the current node goes first and its subnodes follow. With postorder, the current node completes the recursion.

If you hide all the recursion and complexity in an iterator of iterators, that would be a typical Inversion of Control (IoC) paradigm (or is it a pattern already?). The client controls the move to the next node. Since J2SE 5.0 has a beautiful new feature, the foreach loop, the whole picture now looks like this:

    for (File dir : tree(new File("."))) {
      for (File f : files(dir)) {
        System.out.println("  " + f);

Note two methods, tree(File directory) and files(File directory). The former returns all the subdirectories, preorder; the latter returns only files in the specified directory. Why use two loops where one would probably suffice? My practice showed that for a tree operation you would need to do something on the directory itself, not only on its contents. For instance, you scan all the files and create a summary or stats file in the same directory.

The treePostOrder(File file) method serves as an alternative to preorder. Postorder is a relatively rare method, hence the longer name.

If you don't need all files or all subdirectories, you can use two other forms: tree(File file, FileFilter filter) and files(File file, FileFilter filter), which return only "good" files or subdirectories.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date