Java Reference
In-Depth Information
C:\j2sdk1.4.0\src\java\io\FileDescriptor.java is a file last modified Sat Oct 20
01:13:38 BST 2001
C:\j2sdk1.4.0\src\java\io\FileFilter.java is a file last modified Sat Oct 20
01:13:38 BST 2001
C:\j2sdk1.4.0\src\java\io\FileInputStream.java is a file last modified Sat Oct 20
01:13:38 BST 2001
.
.
.
and so on.
How It Works
Our FileListFilter class has two instance variables - name and extension . The name variable
stores the file name prefix, and extension selects file types to be included in a list. The constructor
sets these variables, and the value of either can be omitted when the constructor is called, by specifying
the appropriate argument as null . If you want a really fancy filter, you can have just one argument to
the constructor and specify the filter as *.java , or A*.java , or even A*.j* . You would just need a
bit more code in the constructor or possibly the accept() method to analyze the argument. Our
implementation of the accept() method here only returns true if the file name that is passed to it by
the list() method has initial characters that are identical to name and the file extension is the same as
that stored in extension .
In the modified example, we construct an object of our filter class using the string " F " as the file name
prefix and the string " .java " as the extension. This version of the example will now only list files with
names beginning with F and with the extension .java .
Creating and Modifying Files and Directories
There are methods defined in the File class that you can use to change the physical file by making it
read-only or renaming it. There are also methods that enable you to create files and directories, and to
delete them. The methods that provide these capabilities are:
Method
Description
renameTo(File path)
The file represented by the current object will be renamed to the
path represented by the File object passed as an argument to the
method. Note that this does not change the current File object in
your program - it alters the physical file. Thus, the file that the
File object represents will no longer exist after executing this
method, because the file will have a new name, and possibly will
be located in a different directory. If the file's directory in the new
path is different from the original, the file will be moved. The
method will fail if the directory in the new path for the file does
not exist, or if you don't have write access to it. If the operation is
successful the method will return true . Otherwise, it will return
false
Search WWH ::




Custom Search