org.openbp.common.io
Class FileUtil

java.lang.Object
  extended by org.openbp.common.io.FileUtil

public final class FileUtil
extends java.lang.Object

The FileUtil class provides some additional static file management utility methods not contained in java.io.File. The class contains static method only.

Author:
Heiko Erhardt

Field Summary
static int FILETYPE_ANY
          File type 'any file' (argument for list(java.lang.String, java.lang.String, int))
static int FILETYPE_DIR
          File type 'directory' (argument for list(java.lang.String, java.lang.String, int))
static int FILETYPE_FILE
          File type 'file' (argument for list(java.lang.String, java.lang.String, int))
 
Method Summary
static java.lang.String constructFileName(java.lang.String dir, java.lang.String name, java.lang.String extension)
          Constructs a file name from a directory and extension specification.
static void copy(java.io.File src, java.io.File dest)
          Copy a file or directory.
static void copy(java.io.File src, java.io.File dest, java.io.FilenameFilter filter)
          Copy a file or directory using a file name filter.
static java.io.File createTempFile(java.lang.String directory, java.lang.String prefix, java.lang.String suffix, java.lang.String extension)
          Determines the full path name for a temporary file.
static boolean deleteFile(java.io.File f, java.io.FilenameFilter filter)
          Deletes a file or a directory.
static java.lang.String getBaseName(java.lang.String pathName)
          Gets the base name (file name without directory path and extension) of a path name.
static java.lang.String getBaseNameWithExtension(java.io.File file)
          Gets the base name (file name without directory path but with extension) of a path name.
static java.lang.String getBaseNameWithExtension(java.lang.String pathName)
          Gets the base name (file name without directory path but with extension) of a path name.
static java.lang.String getDirectoryFromFilePath(java.lang.String filePath)
          Gets the full path name of the directory from a file path specification.
static java.io.File getParent(java.io.File f, boolean returnCurrent)
          File.getParent() can return null when the file is specified without a directory or is in the root directory.
static java.lang.String getRelativePath(java.lang.String startPath, java.lang.String targetPath)
          Returns the relative path between two different files.
static java.lang.String getRelativePathToParentDir(java.lang.String target, java.lang.String origin)
          Determines the relative path from the given origin path name to the given target path name, assuming that the target is a sub directory of the origin.
static java.lang.String[] list(java.lang.String dirName, java.lang.String pattern, int fileType)
          Returns an array of strings naming the files and directories in a directory, matching the files against an optional pattern and file type.
static void main(java.lang.String[] args)
          Main method for test purposes.
static void remove(java.io.File src)
          Recursively removes a file or directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILETYPE_ANY

public static final int FILETYPE_ANY
File type 'any file' (argument for list(java.lang.String, java.lang.String, int))

See Also:
Constant Field Values

FILETYPE_FILE

public static final int FILETYPE_FILE
File type 'file' (argument for list(java.lang.String, java.lang.String, int))

See Also:
Constant Field Values

FILETYPE_DIR

public static final int FILETYPE_DIR
File type 'directory' (argument for list(java.lang.String, java.lang.String, int))

See Also:
Constant Field Values
Method Detail

list

public static java.lang.String[] list(java.lang.String dirName,
                                      java.lang.String pattern,
                                      int fileType)
Returns an array of strings naming the files and directories in a directory, matching the files against an optional pattern and file type. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of strings is returned, one for each file or directory in the directory. Names denoting the directory itself and the directory's parent directory are not included in the result. Each string is a file name rather than a complete path. There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Parameters:
dirName - Name of the directory or null for the current directory
pattern - Wildcard pattern (see the ShellMatcher class)
The pattern is case-insensitive.
fileType - Type of file to be listed (FILETYPE_ANY/FILETYPE_FILE/FILETYPE_DIR) KNOWN BUG: It seems that on Win32 systems, it does always return all files and directories matching pattern, regardless of fileType.
Returns:
An array of strings naming the files and directories in the directory. The array will be empty if the directory is empty. Returns null if the pathname does not denote a directory, or if an I/O error occurs.
Throws:
java.lang.SecurityException - If a security manager exists and denies read access to the directory

remove

public static void remove(java.io.File src)
                   throws java.io.IOException
Recursively removes a file or directory. If the file is a directory and is not empty, its sub directories and files will be deleted also.

Parameters:
src - File or directory to delete
Throws:
java.io.IOException - If the file could not be deleted

copy

public static void copy(java.io.File src,
                        java.io.File dest)
                 throws java.io.IOException
Copy a file or directory. Can be used for copying files and/or directories.
For some reason, there is no java.io.File.copy() method, hence this method.
It can be used to copy file2file, file2directory, or directory2directory (recursively).

Parameters:
src - Source file or directory
dest - Destination file or directory
Throws:
java.io.IOException - If the operation fails

copy

public static void copy(java.io.File src,
                        java.io.File dest,
                        java.io.FilenameFilter filter)
                 throws java.io.IOException
Copy a file or directory using a file name filter. Can be used for copying files and/or directories.
For some reason, there is no java.io.File.copy() method, hence this method.
It can be used to copy file2file, file2directory, or directory2directory (recursively).

Parameters:
src - Source file or directory
dest - Destination file or directory
filter - File name filter that determines which files of a directory will be copied
Throws:
java.io.IOException - If the operation fails

deleteFile

public static boolean deleteFile(java.io.File f,
                                 java.io.FilenameFilter filter)
Deletes a file or a directory.

Parameters:
f - The file to delete
filter - The file name filter
Returns:
true The file was deleted successfully
false The deletion failed

getBaseNameWithExtension

public static java.lang.String getBaseNameWithExtension(java.io.File file)
Gets the base name (file name without directory path but with extension) of a path name.

Parameters:
file - The file
Returns:
The base name

getBaseNameWithExtension

public static java.lang.String getBaseNameWithExtension(java.lang.String pathName)
Gets the base name (file name without directory path but with extension) of a path name.

Parameters:
pathName - The path name of the file
Returns:
The base name

getBaseName

public static java.lang.String getBaseName(java.lang.String pathName)
Gets the base name (file name without directory path and extension) of a path name.

Parameters:
pathName - The path name of the file
Returns:
The base name

getDirectoryFromFilePath

public static java.lang.String getDirectoryFromFilePath(java.lang.String filePath)
Gets the full path name of the directory from a file path specification.

Parameters:
filePath - Path specification of the file
Returns:
Directory path or null if the file is in the current directory

getRelativePathToParentDir

public static java.lang.String getRelativePathToParentDir(java.lang.String target,
                                                          java.lang.String origin)
Determines the relative path from the given origin path name to the given target path name, assuming that the target is a sub directory of the origin. For a generic solution if the directories do not relate to each other see the getRelativePath(java.lang.String, java.lang.String) method.

Parameters:
target - Absolute path of the target path (the sub directory, example: "d:\Src\framework\skyfw")
origin - Absolute path of the origin path (the base directory, example: "d:\Src")
Returns:
The relative path or null if 'target' turns out not to be a sub dir of 'origin'

getRelativePath

public static java.lang.String getRelativePath(java.lang.String startPath,
                                               java.lang.String targetPath)
Returns the relative path between two different files. Both parameters have to be absolute paths, but do not need to be parent/sub directories of each other.

Parameters:
startPath - The start directory
targetPath - The directory which should be reached by appending the resulting relative path to the start directory.
Returns:
The relative path
Throws:
java.lang.IllegalArgumentException - if one of the arguments is null or a relative path

constructFileName

public static java.lang.String constructFileName(java.lang.String dir,
                                                 java.lang.String name,
                                                 java.lang.String extension)
Constructs a file name from a directory and extension specification.

Parameters:
dir - Directory to place the file in
name - Base name of the file
extension - The file name extension
Returns:
The path name of the file

createTempFile

public static java.io.File createTempFile(java.lang.String directory,
                                          java.lang.String prefix,
                                          java.lang.String suffix,
                                          java.lang.String extension)
Determines the full path name for a temporary file.

Parameters:
prefix - Prefix for temporary file name or null
suffix - Suffix for temporary file name or null
directory - Directory of temporary file
extension - Extension of temporary file or null
Returns:
A File object specifying the temporary file

getParent

public static java.io.File getParent(java.io.File f,
                                     boolean returnCurrent)
File.getParent() can return null when the file is specified without a directory or is in the root directory. This method handles those cases.

Parameters:
f - The target File to analyze
returnCurrent - true Returns a file object (the current directory) also if the file does not contain a path specification.
false Returns null if the file does not contain a path specification.
Returns:
The parent directory as a File

main

public static void main(java.lang.String[] args)
Main method for test purposes.

Parameters:
args - Argument array


Copyright © 2011. All Rights Reserved.