ariba.util.core
Class FileUtil

java.lang.Object
  extended by ariba.util.core.FileUtil

public final class FileUtil
extends java.lang.Object

File Utilities. These are helper functions for dealing with files.


Nested Class Summary
static class FileUtil.FileDeletionException
          Represents the exceptional condition where we expected to be able to delete a file but were not actually able to do so.
static class FileUtil.FileRenameException
          Represents the exceptional condition where we expected to be able to rename a file but were not actually able to do so.
 
Field Summary
static java.lang.String DefaultBackupFileSuffix
           
static int DefaultMaxBackupNumber
           
 
Method Summary
static void createDirsForFile(java.io.File file)
          Create directories for file if they do not exist.
static void delete(java.io.File toDelete)
          Deletes the file identified by toDelete.
static void deleteAndRename(java.io.File toDelete, java.io.File toRename)
          Convenience method that deletes toDelete and renames toRename to toDelete.
static boolean deleteFile(java.io.File file)
          Convenience method that will call delete() on file and if that fails will call file.deleteOnExit() scheduling the file for deletion upon normal server exit.
static void DeleteFiles(java.io.File dir, java.io.FilenameFilter filter)
          Deletes the files in a specific directory that satisfy the filter.
static void deleteSubdirectoryIfEmpty(java.io.FileFilter purgeTempFileFilter, java.io.File dir, boolean removeSelf)
           
static java.io.File directory(java.io.File dir)
          Returns the File for a directory.
static java.io.File directory(java.io.File base, java.lang.String dir)
          Returns the File for a named directory.
static java.io.File directory(java.lang.String dir)
          Returns the File for a named directory.
static java.io.File directory(java.lang.String base, java.lang.String dir)
          Returns the File for a named directory.
static java.io.File fixFileSeparators(java.io.File file)
          Fix up file separators for platform independence.
static java.lang.String fixFileSeparators(java.lang.String filename)
          Fix up filename separators for platform independence.
static java.io.File getCanonicalFile(java.io.File file)
          Convert a file to canonical form.
static java.util.List getSharedTempDirSnapshot()
          Get a snapshot of the contents of shared temp dir.
static boolean isAbsolutePath(java.lang.String path)
          Determines if the path specified by a given String is an absolute path or not.
static java.io.File[] ListFiles(java.io.File dir, java.io.FilenameFilter filter)
          Lists the files in a specific directory that satisfy the filter.
static java.lang.String makeFileName(java.lang.String prefix, java.lang.String filename)
          Makes a 'standard' helper file name.
static java.io.File mostRecentModifiedFile(java.io.File dir, java.io.FilenameFilter filter)
          Returns the most recently modified file in a specific directory that satisfies the filter.
static java.lang.String parseFileExtension(java.lang.String filename)
          Returns the extension of the given filename.
static java.lang.String parseFileName(java.lang.String path)
          Returns the filename or last name in the path.
static int purgeDir(java.io.File dir, java.io.FileFilter filter)
          Recursively remove all files from the specified directory.
static int purgeDir(java.io.File dir, java.io.FileFilter filter, int minimumAgeInHours)
           
static void purgeLocalTempDir()
          Recursively remove all files from the local temp directory.
static void purgeLocalTempDir(java.io.FileFilter filter, int minimumDirAge)
          Recursively remove all files from the local temp directory.
static void purgeSharedTempDir()
          Recursively remove all files from the shared temp directory.
static void purgeSharedTempDir(java.io.FileFilter filter)
          Recursively remove all files from the shared temp directory.
static void purgeSharedTempDir(java.io.FileFilter filter, int minimumDirAge)
          Recursively remove all files from the shared temp directory.
static void purgeSharedTempDirSnapshot(java.util.List sharedTempDirSnapshot, long baseTime)
          Purge the temp shared dir by only removing the files specified in the snapshot input parameter.
static void rename(java.io.File fromFile, java.io.File toFile)
          Renames fromFile to toFile.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DefaultBackupFileSuffix

public static final java.lang.String DefaultBackupFileSuffix
See Also:
Constant Field Values

DefaultMaxBackupNumber

public static final int DefaultMaxBackupNumber
See Also:
Constant Field Values
Method Detail

fixFileSeparators

public static java.io.File fixFileSeparators(java.io.File file)
Fix up file separators for platform independence. Converts file to use the appropriate separator character for the current platform.

Parameters:
file - a file potentially with non platform specific file separator characters ("/" or "\")
Returns:
the same file but with only File.separatorChar as the separator

fixFileSeparators

public static java.lang.String fixFileSeparators(java.lang.String filename)
Fix up filename separators for platform independence. Converts filename to use the appropriate separator character for the current platform.

Parameters:
filename - a filename potentially with non platform specific file separator characters ("/" or "\")
Returns:
the same filename but with only File.separatorChar as the separator

directory

public static java.io.File directory(java.io.File base,
                                     java.lang.String dir)
                              throws java.io.IOException
Returns the File for a named directory. If there is no directory dir relative to the File base, it and all needed parent directories will be created.

Parameters:
base - the parent directory of dir
dir - the name of the directory to find or create
Returns:
the directory found or created
Throws:
java.io.IOException - if there was an error finding or creating the directory

directory

public static java.io.File directory(java.lang.String base,
                                     java.lang.String dir)
                              throws java.io.IOException
Returns the File for a named directory. If there is no directory dir relative to the directory base, it and all needed parent directories will be created.

Parameters:
base - the parent directory of dir
dir - the name of the directory to find or create
Returns:
the directory found or created
Throws:
java.io.IOException - if there was an error finding or creating the directory

directory

public static java.io.File directory(java.lang.String dir)
                              throws java.io.IOException
Returns the File for a named directory. If there is no directory it and all needed parent directories will be created.

Parameters:
dir - the name of the directory to find or create
Returns:
the directory found or created
Throws:
java.io.IOException - if there was an error finding or creating the directory

directory

public static java.io.File directory(java.io.File dir)
                              throws java.io.IOException
Returns the File for a directory. If there is no directory it and all needed parent directories will be created.

Parameters:
dir - the directory to find or create
Returns:
the directory found or created
Throws:
java.io.IOException - if there was an error finding or creating the directory

getCanonicalFile

public static java.io.File getCanonicalFile(java.io.File file)
Convert a file to canonical form. On error, returns the same file. Java doesn't specify what might cause a failure. Most likely it is if you have permissions problems on the parent directories.

Parameters:
file - the file to convert to canonical form
Returns:
current file converted to canonical format

createDirsForFile

public static void createDirsForFile(java.io.File file)
                              throws java.io.IOException
Create directories for file if they do not exist.

Parameters:
file - the file containing possibly non-existent directories
Throws:
java.io.IOException - if there was an error finding or creating the directory

deleteFile

public static boolean deleteFile(java.io.File file)
Convenience method that will call delete() on file and if that fails will call file.deleteOnExit() scheduling the file for deletion upon normal server exit.

Parameters:
file - the File to delete
Returns:
true if file was immediately deleted and false otherwise

delete

public static void delete(java.io.File toDelete)
                   throws FileUtil.FileDeletionException
Deletes the file identified by toDelete.

Throws:
FileUtil.FileDeletionException - if toDelete cannot be deleted

rename

public static void rename(java.io.File fromFile,
                          java.io.File toFile)
                   throws FileUtil.FileRenameException
Renames fromFile to toFile.

Throws:
FileUtil.FileRenameException - if fromFile cannot be renamed to toFile

deleteAndRename

public static void deleteAndRename(java.io.File toDelete,
                                   java.io.File toRename)
                            throws FileUtil.FileDeletionException,
                                   FileUtil.FileRenameException
Convenience method that deletes toDelete and renames toRename to toDelete.

Throws:
FileUtil.FileDeletionException - if toDelete cannot be deleted
FileUtil.FileRenameException - if toRename cannot be renamed (assuming toDelete is deleted)

isAbsolutePath

public static boolean isAbsolutePath(java.lang.String path)
Determines if the path specified by a given String is an absolute path or not. On UNIX systems, a pathname is absolute if its prefix is "/". On Win32 systems, a pathname is absolute if its prefix is a drive specifier followed by "\\", or if its prefix is "\\".

Parameters:
path - the input String.
Returns:
true if the path is an absolute path. false otherwise. Returns false if the input string is null or empty or blank.

purgeLocalTempDir

public static void purgeLocalTempDir()
Recursively remove all files from the local temp directory. This unfiltered method is here as a convenience for code (e.g. startup stuff) that wants to remove the files unconditionally.

Throws:
java.lang.SecurityException - if some files don't have read access.

purgeLocalTempDir

public static void purgeLocalTempDir(java.io.FileFilter filter,
                                     int minimumDirAge)
Recursively remove all files from the local temp directory. Use the supplied filter to select a subset of the files.

Parameters:
filter - the selection filter for the directory and its subdirectories; May be null to indicate that all files are to be deleted.
Throws:
java.lang.SecurityException - if some files don't have read access.

purgeSharedTempDir

public static void purgeSharedTempDir()
Recursively remove all files from the shared temp directory. This unfiltered method is here as a convenience for code (e.g. startup stuff) that wants to remove the files unconditionally.

Throws:
java.lang.SecurityException - if some files don't have read access.

purgeSharedTempDir

public static void purgeSharedTempDir(java.io.FileFilter filter)
Recursively remove all files from the shared temp directory. Use the supplied filter to select a subset of the files.

Parameters:
filter - the selection filter for the directory and its subdirectories; May be null to indicate that all files are to be deleted.
Throws:
java.lang.SecurityException - if some files don't have read access.

purgeSharedTempDir

public static void purgeSharedTempDir(java.io.FileFilter filter,
                                      int minimumDirAge)
Recursively remove all files from the shared temp directory. Use the supplied filter to select a subset of the files. Use minimumDirAge to specify minimum dir age eligible for deletion.

Parameters:
filter - the selection filter for the directory and its subdirectories; May be null to indicate that all files are to be deleted.
Throws:
java.lang.SecurityException - if some files don't have read access.

getSharedTempDirSnapshot

public static java.util.List getSharedTempDirSnapshot()
Get a snapshot of the contents of shared temp dir. The result will be a List of File objects representing the tree structure of the shared temp dir. depending on the definition of the path in shared temp dir the snapshot will include File objects with relative or absolute paths.

Returns:
a list of File objects that represent a tree structure of the shared temp dir

purgeSharedTempDirSnapshot

public static void purgeSharedTempDirSnapshot(java.util.List sharedTempDirSnapshot,
                                              long baseTime)
Purge the temp shared dir by only removing the files specified in the snapshot input parameter. The files will be removed if they have not beed modified after the baseTime. The reason that we have this method is to enable purging temp shared dir (or any directory) asynchronously. Meaning we only remove the files that we first got a snapshot of. This way the new files created by concurrent threads will not be deleted. By checking the last modified property of the file against the base line we make sure that the method does not delete the files that are being used since the time that we got the snapshot.

Parameters:
sharedTempDirSnapshot - the list of the files inthe snapshot to be removed. Note that the files can have relative or absolute paths. The directories will not be removed.
baseTime - a base time to be used for delteing the files. Files that are mmodified after this base time will not be removed.

purgeDir

public static int purgeDir(java.io.File dir,
                           java.io.FileFilter filter)
Recursively remove all files from the specified directory. Use the supplied filter to select a subset of the files.

Parameters:
dir - the root of the deletion process
filter - the selection filter for the directory and its subdirectories; May be null to indicate that all files are to be deleted.
Returns:
the number of files actually deleted
Throws:
java.lang.SecurityException - if some files don't have read access.

purgeDir

public static int purgeDir(java.io.File dir,
                           java.io.FileFilter filter,
                           int minimumAgeInHours)

deleteSubdirectoryIfEmpty

public static void deleteSubdirectoryIfEmpty(java.io.FileFilter purgeTempFileFilter,
                                             java.io.File dir,
                                             boolean removeSelf)

ListFiles

public static java.io.File[] ListFiles(java.io.File dir,
                                       java.io.FilenameFilter filter)
Lists the files in a specific directory that satisfy the filter.

Parameters:
dir - the directory containig the listing files
filter - the selection filter for the files to be listed; May be null, then all files in the dir will be listed
Returns:
the file list

DeleteFiles

public static void DeleteFiles(java.io.File dir,
                               java.io.FilenameFilter filter)
Deletes the files in a specific directory that satisfy the filter.

Parameters:
dir - the directory containig the files to be deleted
filter - the selection filter for the files to be deleted; May be null, then all files in the dir will be deleted

mostRecentModifiedFile

public static java.io.File mostRecentModifiedFile(java.io.File dir,
                                                  java.io.FilenameFilter filter)
Returns the most recently modified file in a specific directory that satisfies the filter.

Parameters:
dir - the directory containig the listing files
filter - the selection filter for the files to be listed; May be null, then all files in the dir will be considerd
Returns:
the most recently modified file that satisfies the filter

makeFileName

public static java.lang.String makeFileName(java.lang.String prefix,
                                            java.lang.String filename)
Makes a 'standard' helper file name. by prepending the prefix, and then stripping off the directory name of the filename (if any).

Parameters:
prefix -
filename -
Returns:
non-null string.

parseFileName

public static java.lang.String parseFileName(java.lang.String path)
Returns the filename or last name in the path. Handles both forward and backward slashes

Parameters:
path -
Returns:
String or null if path is null.

parseFileExtension

public static java.lang.String parseFileExtension(java.lang.String filename)
Returns the extension of the given filename. The extension is the separated from the file name by a period.

Parameters:
filename -
Returns:
the extension or null if filename is null or does not contain a period.


AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.