Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

The File class: A directory lister

java



+ Font mai mare | - Font mai mic



The File class

The File class has a deceiving name - you might think it refers to a file, but it doesn't. It can represent either the name of a particular file or the names of a set of files in a directory. If it's a set of files, you can ask for the set with the list( ) method, and this returns an array of String. It makes sense to return an array rather than one of the flexible collection classes because the number of elements is fixed, and if you want a different directory listing you just create a different File object. In fact, "FilePath" would have been a better name. This section shows a complete example of the use of this class, including the associated FilenameFilter interface.



A directory lister

Suppose you'd like to see a directory listing. The File object can be listed in two ways. If you call list( ) with no arguments, you'll get the full list that the File object contains. However, if you want a restricted list, for example, all of the files with an extension of .java, then you use a "directory filter," which is a class that tells how to select the File objects for display.

Here's the code for the example: (See page if you have trouble executing this program.)

//: DirList.java

// Displays directory listing

package c10;

import java.io.*;

public class DirList catch(Exception e)

}

}

class DirFilter implements FilenameFilter

public boolean accept(File dir, String name)

} ///:~

The DirFilter class "implements" the interface FilenameFilter. (Interfaces were covered in Chapter 7.) It's useful to see how simple the FilenameFilter interface is:

public interface FilenameFilter

It says that all that this type of object does is provide a method called accept( ). The whole reason behind the creation of this class is to provide the accept( ) method to the list( ) method so that list( ) can call back accept( ) to determine which file names should be included in the list. Thus, this technique is often referred to as a callback or sometimes a functor (that is, DirFilter is a functor because its only job is to hold a method). Because list( ) takes a FilenameFilter object as its argument, it means that you can pass an object of any class that implements FilenameFilter to choose (even at run-time) how the list( ) method will behave. The purpose of a callback is to provide flexibility in the behavior of code.

DirFilter shows that just because an interface contains only a set of methods, you're not restricted to writing only those methods. (You must at least provide definitions for all the methods in an interface, however.) In this case, the DirFilter constructor is also created.

The accept( ) method must accept a File object representing the directory that a particular file is found in, and a String containing the name of that file. You might choose to use or ignore either of these arguments, but you will probably at least use the file name. Remember that the list( ) method is calling accept( ) for each of the file names in the directory object to see which one should be included - this is indicated by the boolean result returned by accept( ).

To make sure that what you're working with is only the name and contains no path information, all you have to do is take the String object and create a File object out of it, then call getName( ) which strips away all the path information (in a platform-independent way). Then accept( ) uses the String class indexOf( ) method to see if the search string afn appears anywhere in the name of the file. If afn is found within the string, the return value is the starting index of afn, but if it's not found the return value is -1. Keep in mind that this is a simple string search and does not have regular expression "wildcard" matching such as "fo?.b?r*" which is much more difficult to implement.

The list( ) method returns an array. You can query this array for its length and then move through it selecting the array elements. This ability to easily pass an array in and out of a method is a tremendous improvement over the behavior of C and C++.

Anonymous inner classes

This example is ideal for rewriting using an anonymous inner class (described in Chapter 7). As a first cut, a method filter( ) is created that returns a handle to a FilenameFilter:

//: DirList2.java

// Uses Java 1.1 anonymous inner classes

import java.io.*;

public class DirList2

}; // End of anonymous inner class

}

public static void main(String[] args) catch(Exception e)

}

} ///:~

Note that the argument to filter( ) must be final. This is required by the anonymous inner class so that it can use an object from outside its scope.

This design is an improvement because the FilenameFilter class is now tightly bound to DirList2. However, you can take this approach one step further and define the anonymous inner class as an argument to list( ), in which case it's even smaller:

//: DirList3.java

// Building the anonymous inner class 'in-place'

import java.io.*;

public class DirList3

});

for(int i = 0; i < list.length; i++)

System.out.println(list[i]);

} catch(Exception e)

}

} ///:~

The argument to main( ) is now final, since the anonymous inner class uses args[0] directly.

This shows you how anonymous inner classes allow the creation of quick-and-dirty classes to solve problems. Since everything in Java revolves around classes, this can be a useful coding technique. One benefit is that it keeps the code that solves a particular problem isolated together in one spot. On the other hand, it is not always as easy to read, so you must use it judiciously.

A sorted directory listing

Ah, you say that you want the file names sorted? Since there's no support for sorting in Java 1.0 or Java 1.1 (although sorting is included in Java 1.2), it will have to be added into the program directly using the SortVector created in Chapter 8:

//: SortedDirList.java

// Displays sorted directory listing

import java.io.*;

import c08.*;

public class SortedDirList

});

sort();

}

void print()

private void sort()

// Test it:

public static void main(String[] args)

} ///:~

A few other improvements have been made. Instead of creating path and list as local variables to main( ), they are members of the class so their values can be accessible for the lifetime of the object. In fact, main( ) is now just a way to test the class. You can see that the constructor of the class automatically sorts the list once that list has been created.

The sort is case-insensitive so you don't end up with a list of all the words starting with capital letters, followed by the rest of the words starting with all the lowercase letters. However, you'll notice that within a group of file names that begin with the same letter the capitalized words are listed first, which is still not quite the desired behavior for the sort. This problem will be fixed in Java 1.2.

Checking for and creating directories

The File class is more than just a representation for an existing directory path, file, or group of files. You can also use a File object to create a new directory or an entire directory path if it doesn't exist. You can also look at the characteristics of files (size, last modification date, read/write), see whether a File object represents a file or a directory, and delete a file. This program shows the remaining methods available with the File class:

//: MakeDirectories.java

// Demonstrates the use of the File class to

// create directories and manipulate files.

import java.io.*;

public class MakeDirectories

private static void fileData(File f)

public static void main(String[] args)

int count = 0;

boolean del = false;

if(args[0].equals('-d'))

for( ; count < args.length; count++)

}

else

}

fileData(f);

}

}

} ///:~

In fileData( ) you can see the various file investigation methods put to use to display information about the file or directory path.

The first method that's exercised by main( ) is renameTo( ), which allows you to rename (or move) a file to an entirely new path represented by the argument, which is another File object. This also works with directories of any length.

If you experiment with the above program, you'll find that you can make a directory path of any complexity because mkdirs( ) will do all the work for you. In Java 1.0, the -d flag reports that the directory is deleted but it's still there; in Java 1.1 the directory is actually deleted.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 643
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved