This issue presents tips, techniques, and sample code for the following topics:
Setting File Modification Times
One of the Java programming language's strengths (and weaknesses) is
that the language is somewhat insulated from the underlying operating
system. java.io.File is one of the classes that bridges this gap, and
for JDKTM 1.2, some important features have been added.
One JDK 1.2 feature is the ability to set the modification time on a file.
This feature has various uses, an important one being the ability to change
the modification time of a source file in relation to files derived from
that file (such as object files). This type of interaction is standard
when you're performing software builds. The UNIX "touch"
command offers this functionality, and files that are "touched"
have their modification time set to the current time. This feature is
sometimes implemented by reading the first byte of the file and then
writing it back, but a cleaner operation is to rely on a system service
for this purpose.
With JDK 1.2 a new method has been added to File:
"setLastModified". To see how this method works, consider the
following utility program that is the equivalent of touch:
import java.io.*;
public class touch {
public static void main(String args[])
{
long curr_time = System.currentTimeMillis();
for (int i = 0; i < args.length; i++) {
File f = new File(args[i]);
boolean b = f.setLastModified(curr_time);
if (!b)
System.err.println("touch failed: "
+ args[i]);
}
}
}
This program iterates over its file arguments, and sets the modification
time of each file to the current time. setLastModified returns false if
the time cannot be set. Times are measured in milliseconds since the
starting point (January 1 1970 00:00:00 GMT), though a given underlying
operating system may only support precision to the nearest second.
Walking across File Systems
Another quite important feature of JDK 1.2 file support is the ability
to obtain a list of all the file system roots. With UNIX there is only
one root for file-naming purposes, "/". But on Windows systems,
there are multiple roots, one for each partition or logical drive. The roots
have names like "C:" or "G:".
File.listRoots is a static method that can be used to obtain all the roots.
listRoots is especially useful if you need to iterate across all the files
on a PC to find particular files by name or contents.
On a system running Windows NT 4.0 with multiple physical drives and
partitions, listRoots returns a list similar to the following:
A:\
C:\
D:\
E:\
F:\
G:\
J:\
K:\
M:\
P:\
R:\
S:\
T:\
W:\
Z:\
An example of using listRoots is the following program that displays every
directory and file name located on a local system:
import java.io.*;
public class roots {
public static void visit(File f)
{
System.out.println(f);
}
public static void walk(File f)
{
visit(f);
if (f.isDirectory()) {
String list[] = f.list();
for (int i = 0; i < list.length; i++)
walk(new File(f, list[i]));
}
}
public static void main(String args[])
{
File list[] = File.listRoots();
for (int i = 0; i < list.length; i++) {
if (list[i].exists())
walk(list[i]);
else
System.err.println("not accessible:"
+ list[i]);
}
}
}
The program uses listRoots to find all the file system roots, and then
recursively walks through each one, expanding directories as they are
found. An example of the first few lines of output on the same Windows NT
4.0 system mentioned above would be:
C:\
C:\.hotjava
C:\.hotjava\properties
C:\boot.ini
C:\bootsect.lnx
C:\IO.SYS
C:\MSDOS.SYS
C:\NTDETECT.COM
C:\ntldr
C:\old
The total output on the Windows NT 4.0 system is about 59,000 lines. Note
that listRoots may return the names of some roots that are not immediately
accessible, for example A: in reference to an empty floppy drive.
|