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 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 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[] 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 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

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


Copyright © 2011. All Rights Reserved.