Creating Files With Unique Names

A frequent need arises to create files with unique names matching some pattern. Although Java provides some services for creating unique file names, the engine provides additional services to create file names meeting specific design patterns.

Patterns supported include serial number and time stamp. Each is represented in the pattern by a character: # for serial number and * for time stamp. The serial number created in response to the # is sized according to the number of # characters with rollover to 1. For example, MYFIL###.OUT generates unique files from MYFIL001.OUT to MYFILE999.OUT.

To create a unique file name, you first create a pattern object XDUniqueFile with the pattern you desire:

void XDUniqueFile(String directory, String pattern)

and then call nextFile() on the pattern:

String dir = "SREG(iwayhome)/myarea"; // this might come from parameters
dir = XDUtils.evaluate(dir,null,this); 
XDUniqueFile uf = new XDUniqueFile(dir,"F###.TXT");
File useFile = uf.nextFile();

The supported meta characters are listed below.

Name

Description

*

Current timestamp in milliseconds.

#

Serializing number, modulus the antilog of the number of consecutive # characters. For example, AB## becomes AB00..AB99, and then recycles to AB00. The number is stored on a disk, and is continuous for any specific pattern across starts.

^

Similar to the # character, but not stored on a disk. This restarts at 0 each time the system starts, and applies irrespective of any pattern. Faster than # as it avoids a disk access.