JavaJournalJanuary 15, 1999 |
|
after nearly two
years of writing applets, the author discovers that Java
has significant power in manipulating files.
|
IO, IO It's off to work I go... My first experience with the java.io package was a recent brush with the File class. This is odd, because I've been coding Java for almost two years. But since a lot of this code was of the Applet variety of code, I never had a lot of occasion to hit the local file system. That is until recently, when I unzipped the JDK 1.2 (rounded off recently to Java 2) and discovered that every single class I wrote in the last six months would have to be modified. I needed to change the imports from com.sun.java.swing to javax. The folks at Sun provide a small java file which takes care of the search and replace effort. The small catch is that this class takes care of ONE FILE ONLY. I needed to persuade this code to visit and purify more than 500 classes in a dozen packages. I wanted to see if a recent project which clipped along under JDK 1.1.6 would pick up any steam under Java 2. File is a class that provides a number of native calls into the Virtual Machine's underlying operating system. The methods are few, but they seem to handle to majority of file and directory operations that you would want to perform. The constructor of file, by accepting a string that may be either a file or a path is able to represent either a directory or a file in a single instance. You query the object instantianted with the string passed to the constructor, through an isFile() or isDirectory() method. A method called list() returns a collection of files and sub-directories in a string array. The string array, of course, almost begs the coder to continue creating File objects. This is the sort of capability that would allow a coder to start with a File object like new File("C:\\") and start recursing through the entire file system. Thus, the method that deals with directories, when it finds a File object of directory type, calls itself with the new directory. And will continue to do so until it runs out of directory-type File objects. My own recollection of recursing through file systems has not been pretty. I remember trying this in 16 bit Windows using VB 3.0, only to be quickly discouraged by a series of stack errors that would happen faithfully every time the directory structure needed to recurse to more than 20K. Not so with Java. I'm not saying there is no upper limit. All I'm saying is that I never reached it after scouring through several hefty file systems. private void doDirectory(String path) {
int iArg = 0;
File dir = new File(path);
String args[];
args = dir.list();
if (args == null ){
error("no files specified");
}
int nArgs = args.length;
if (nArgs == 0) {
usage();
}
for (; iArg < nArgs; iArg++) {
String fileName = path + args[iArg];
File temp = new File(path + args[iArg]);
if (temp.isDirectory()){
this.doDirectory(fileName + "\\");
} else if (temp.isFile()){
this.doFile(fileName);
}
}
}
Copyright (c) Gervase Gallant 1999. |