# JDC Tech Tips Vol. 2 No. 4

JDCTechTips@sun.com
Tue, 17 Nov 1998 06:35:26 GMT

- Messages sorted by: [ date ][ thread ][ subject ][ author ]
- Next message: Paul: "Between our.."
- Previous message: Bernard Leach: "ABA releases JCE crypto package"

-WELCOME- to the Java(sm) Developer Connection(sm) Tech Tips. This issue
covers JDK(tm) 1.2 features for performing directory and file manipulation.
The JDC Team-

J D C T E C H T I P S
TIPS, TECHNIQUES, AND SAMPLE CODE
* Setting File Modification Times
* Walking across File Systems
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
T I P S , T E C H N I Q U E S , A N D S A M P L E C O D E

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 JDK 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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-- NOTE --

The names on the JDC mailing list are used for internal Sun
Microsystems(tm) purposes only. To remove your name from the list, see
Subscribe/Unsubscribe below.

-- FEEDBACK --

Comments? Send your feedback on the JDC Tech Tips to:

JDCTechTips@Sun.com

-- SUBSCRIBE/UNSUBSCRIBE --

The JDC Tech Tips are sent to you because you elected to subscribe when you
registered as a JDC member. To unsubscribe from JDC Email, go to the
following address and enter the email address you wish to remove from the
mailing list:

http://developer.java.sun.com/unsubscribe.html

-- ARCHIVES --

You'll find the JDC Tech Tips archives at:

http://developer.java.sun.com/developer/javaInDepth/TechTips/index.html

-- COPYRIGHT --

Copyright 1998 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.

This document is protected by copyright. For more information, see:

http://developer.java.sun.com/developer/copyright.html

The JDC Tech Tips are written by Glen McCluskey.

JDC Tech Tips Vol. 2 No. 4
November 16, 1998

- Next message: Paul: "Between our.."
- Previous message: Bernard Leach: "ABA releases JCE crypto package"