com.vladium.util
Class Files

java.lang.Object
  extended bycom.vladium.util.Files

public abstract class Files
extends java.lang.Object

Author:
Vlad Roubtsov, (C) 2003

Constructor Summary
private Files()
           
 
Method Summary
static java.io.File canonicalizeFile(java.io.File file)
           
static java.lang.String canonicalizePathname(java.lang.String pathname)
          Converts 'pathname' into the canonical OS form.
static java.io.File createTempFile(java.io.File parentDir, java.lang.String prefix, java.lang.String extension)
          A slightly stricter version of File.createTempFile() in J2SDK 1.3: it requires that the caller provide an existing parent directory for the temp file.
static java.lang.String getFileExtension(java.io.File file)
          Invariant: (getFileName (file) + getFileExtension (file)).equals (file.getName ()).
static java.lang.String getFileName(java.io.File file)
          Invariant: (getFileName (file) + getFileExtension (file)).equals (file.getName ()).
static java.io.File newFile(java.io.File dir, java.io.File file)
           
static java.io.File newFile(java.io.File dir, java.lang.String file)
           
static java.io.File newFile(java.lang.String dir, java.lang.String file)
           
static java.io.File[] pathToFiles(java.lang.String[] path, boolean canonical)
          Converts an array of path segments to an array of Files.
static java.lang.String[] readFileList(java.io.File atfile)
          No duplicate elimination.
static boolean renameFile(java.io.File source, java.io.File target, boolean overwrite)
          Renames 'source' to 'target' [intermediate directories are created if necessary].
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Files

private Files()
Method Detail

readFileList

public static java.lang.String[] readFileList(java.io.File atfile)
                                       throws java.io.IOException
No duplicate elimination.

Parameters:
atfile -
Throws:
java.io.IOException

pathToFiles

public static java.io.File[] pathToFiles(java.lang.String[] path,
                                         boolean canonical)
Converts an array of path segments to an array of Files. The order of files follows the original order of path segments, except "duplicate" entries are removed. The definition of duplicates depends on 'canonical': Note that duplicate removal in classpaths affects ClassLoader.getResources(). The first mode above makes the most sense, however the last one is what Sun's java.net.URLClassLoader appears to do. Hence the last mode might be necessary for reproducing its behavior in Sun-compatible JVMs.


canonicalizePathname

public static java.lang.String canonicalizePathname(java.lang.String pathname)
Converts 'pathname' into the canonical OS form. This wrapper function will return the absolute form of 'pathname' if File.getCanonicalPath() fails.


canonicalizeFile

public static java.io.File canonicalizeFile(java.io.File file)

getFileName

public static java.lang.String getFileName(java.io.File file)
Invariant: (getFileName (file) + getFileExtension (file)).equals (file.getName ()).

Parameters:
file - File input file descriptor [must be non-null]
Returns:
String file name without the extension [excluding '.' separator] [if 'file' does not appear to have an extension, the full name is returned].
Throws:
java.lang.IllegalArgumentException - if 'file' is null

getFileExtension

public static java.lang.String getFileExtension(java.io.File file)
Invariant: (getFileName (file) + getFileExtension (file)).equals (file.getName ()).

Parameters:
file - File input file descriptor [must be non-null]
Returns:
String extension [including '.' separator] or "" if 'file' does not appear to have an extension.
Throws:
java.lang.IllegalArgumentException - if 'file' is null

newFile

public static java.io.File newFile(java.io.File dir,
                                   java.io.File file)
Parameters:
dir - [null is ignored]
file - [absolute overrides 'dir']
Returns:

newFile

public static java.io.File newFile(java.io.File dir,
                                   java.lang.String file)
Parameters:
dir - [null is ignored]
file - [absolute overrides 'dir']
Returns:

newFile

public static java.io.File newFile(java.lang.String dir,
                                   java.lang.String file)
Parameters:
dir - [null is ignored]
file - [absolute overrides 'dir']
Returns:

renameFile

public static boolean renameFile(java.io.File source,
                                 java.io.File target,
                                 boolean overwrite)
Renames 'source' to 'target' [intermediate directories are created if necessary]. If 'target' exists and 'overwrite' is false, the method is a no-op. No exceptions are thrown except for when input is invalid. If the operation fails half-way it can leave some file system artifacts behind.

Parameters:
source - file descriptor [file must exist]
target - target file descriptor [an existing target may get deleted if 'overwrite' is true]
overwrite - if 'true', forces an existing target to be deleted
Returns:
true iff the renaming was actually performed.
Throws:
java.lang.IllegalArgumentException - if 'source' is null or file does not exist
java.lang.IllegalArgumentException - if 'target' is null

createTempFile

public static java.io.File createTempFile(java.io.File parentDir,
                                          java.lang.String prefix,
                                          java.lang.String extension)
                                   throws java.io.IOException
A slightly stricter version of File.createTempFile() in J2SDK 1.3: it requires that the caller provide an existing parent directory for the temp file. This defers to File.createTempFile (prefix, extension, parentDir) after normalizing 'extension'.

MT-safety: if several threads use this API concurrently, the temp files created are guaranteed to get created without any collisions and correspond to files that did not exist before. However, if such a temp file is deleted at a later point, this method may reuse its file name. These MT-safety guarantees do not hold if files are created in the same directory outside of this method.

Parameters:
parentDir - parent dir for the temp file [may not be null and must exist]
prefix - prefix pattern for the temp file name [only the first 3 chars are guaranteed to be used]
extension - pattern for the temp file name [null is equivalient to ".tmp"; this is always normalized to start with "."; only the first 3 non-"." chars are guaranteed to be used]
Returns:
writeable temp file descriptor [incorporates 'parentDir' in its pathname]
Throws:
java.io.IOException - if a temp file could not be created