ariba.util.core
Class FileUtil

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

public final class FileUtil
extends 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 String DefaultBackupFileSuffix
           
static int DefaultMaxBackupNumber
           
 
Method Summary
static void createDirsForFile(File file)
          Create directories for file if they do not exist.
static void delete(File toDelete)
          Deletes the file identified by toDelete.
static void deleteAndRename(File toDelete, File toRename)
          Convenience method that deletes toDelete and renames toRename to toDelete.
static boolean deleteFile(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(File dir, FilenameFilter filter)
          Deletes the files in a specific directory that satisfy the filter.
static File directory(File dir)
          Returns the File for a directory.
static File directory(File base, String dir)
          Returns the File for a named directory.
static File directory(String dir)
          Returns the File for a named directory.
static File directory(String base, String dir)
          Returns the File for a named directory.
static File fixFileSeparators(File file)
          Fix up file separators for platform independence.
static String fixFileSeparators(String filename)
          Fix up filename separators for platform independence.
static File getCanonicalFile(File file)
          Convert a file to canonical form.
static List getSharedTempDirSnapshot()
          Get a snapshot of the contents of shared temp dir.
static boolean isAbsolutePath(String path)
          Determines if the path specified by a given String is an absolute path or not.
static File[] ListFiles(File dir, FilenameFilter filter)
          Lists the files in a specific directory that satisfy the filter.
static File mostRecentModifiedFile(File dir, FilenameFilter filter)
          Returns the most recently modified file in a specific directory that satisfies the filter.
static int purgeDir(File dir, FileFilter filter)
          Recursively remove all files from the specified directory.
static void purgeLocalTempDir()
          Recursively remove all files from the local temp directory.
static void purgeLocalTempDir(FileFilter filter)
          Recursively remove all files from the local temp directory.
static void purgeSharedTempDir()
          Recursively remove all files from the shared temp directory.
static void purgeSharedTempDir(FileFilter filter)
          Recursively remove all files from the shared temp directory.
static void purgeSharedTempDirSnapshot(List sharedTempDirSnapshot, long baseTime)
          Purge the temp shared dir by only removing the files specified in the snapshot input parameter.
static void rename(File fromFile, 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 String DefaultBackupFileSuffix
See Also:
Constant Field Values

DefaultMaxBackupNumber

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

fixFileSeparators

public static File fixFileSeparators(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 String fixFileSeparators(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 File directory(File base,
                             String dir)
                      throws 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:
IOException - if there was an error finding or creating the directory

directory

public static File directory(String base,
                             String dir)
                      throws 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:
IOException - if there was an error finding or creating the directory

directory

public static File directory(String dir)
                      throws 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:
IOException - if there was an error finding or creating the directory

directory

public static File directory(File dir)
                      throws 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:
IOException - if there was an error finding or creating the directory

getCanonicalFile

public static File getCanonicalFile(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(File file)
                              throws IOException
Create directories for file if they do not exist.

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

deleteFile

public static boolean deleteFile(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(File toDelete)
                   throws FileUtil.FileDeletionException
Deletes the file identified by toDelete.

Throws:
FileUtil.FileDeletionException - if toDelete cannot be deleted

rename

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

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

deleteAndRename

public static void deleteAndRename(File toDelete,
                                   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(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:
SecurityException - if some files don't have read access.

purgeLocalTempDir

public static void purgeLocalTempDir(FileFilter filter)
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:
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:
SecurityException - if some files don't have read access.

purgeSharedTempDir

public static void purgeSharedTempDir(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:
SecurityException - if some files don't have read access.

getSharedTempDirSnapshot

public static 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(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(File dir,
                           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:
SecurityException - if some files don't have read access.

ListFiles

public static File[] ListFiles(File dir,
                               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(File dir,
                               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 File mostRecentModifiedFile(File dir,
                                          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


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