ch.aplu.util
Class FilePath

java.lang.Object
  extended by ch.aplu.util.FilePath

public class FilePath
extends java.lang.Object

FilePath is a helper class that simplifies file operations.


Method Summary
static void closeInputStream(java.io.InputStream is)
          Closes the given input stream.
static void closeOutputStream(java.io.OutputStream os)
          Closes the given output stream.
static boolean copyDirectory(java.io.File srcDir, java.io.File destDir, boolean collect)
          Copies all files from the source directory into the destination directory.
static boolean copyFile(java.io.File src, java.io.File dest)
          Copies binary data from the source file to the destination file.
static boolean copyFile(java.net.URL url, java.io.File dest)
          Copies binary data from the source URL to the destination file.
static boolean copyTree(java.io.File srcDir, java.io.File destDir)
          Copies all files with subdirectory structure from the source directory to the destination directory.
static boolean createDirectoryTree(java.io.File dirPath)
          Creates a directory structure.
static int getNbFiles(java.io.File dirPath)
          Returns number of files (subdirectories are not counted) in given directory.
static boolean isDirectoryEmpty(java.io.File dirPath)
          Returns true, if the directory is empty (no files and no subdirectories).
static java.io.InputStream openResource(java.lang.String relPath)
          Loads a binary resource from the application JAR file,
e.g. get a BufferedImage reference from a image resource with bi = ImageIO.read(openResource(path)).
static boolean pack(java.io.File jarFile, java.io.File dirPath)
          Creates a compressed JAR archive from a all files and subdirectories in directory with given dirPath.
static java.lang.String readTextFile(java.io.File filePath)
          Reads a text file into a string.
static java.lang.String readTextResource(java.lang.String relPath)
          Reads a text resource from the application JAR file.
static boolean removeDir(java.io.File dirPath)
          (Be very careful) Deletes all files and subdirectories and the directory itself.
static boolean removeFiles(java.io.File dirPath, boolean clean)
          (Be very careful) Deletes all files in directory (but not the directory itself).
static boolean unpack(java.io.File jarFile, java.io.File dirPath)
          Unpacks the given JAR archive in the given directory.
static boolean unzip(java.io.File zipFile, java.io.File dirPath)
          Unpacks the given ZIP archive in the given directory.
static boolean writeTextFile(java.io.File filePath, java.lang.String text, boolean createTree)
          Writes the given string to the given destination file.
static boolean zip(java.io.File zipFile, java.io.File dirPath)
          Creates a ZIP archive from a all files and subdirectories in directory with given dirPath.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

writeTextFile

public static boolean writeTextFile(java.io.File filePath,
                                    java.lang.String text,
                                    boolean createTree)
Writes the given string to the given destination file. An existing file is overwritten. If createTree is true and filePath contains subdirectories that are nonexistant, the subdirectory directory tree is created.

Parameters:
filePath - a relative or absolute path to the file
text - the text to write
createTree - if true, a nonexistent subdirectory structure is created
Returns:
true, if the file is successfully written; otherwise false

readTextFile

public static java.lang.String readTextFile(java.io.File filePath)
Reads a text file into a string.

Parameters:
filePath - a relative or absolute path to the file
Returns:
the string containing the file data; null, if the the process fails

readTextResource

public static java.lang.String readTextResource(java.lang.String relPath)
Reads a text resource from the application JAR file.

Parameters:
relPath - a relative path to the resource file starting from the root of the JAR archive
Returns:
the String containing the file data; null, if the process fails

openResource

public static java.io.InputStream openResource(java.lang.String relPath)
Loads a binary resource from the application JAR file,
e.g. get a BufferedImage reference from a image resource with bi = ImageIO.read(openResource(path)).

Parameters:
relPath - a relative path to the resource file starting from the root of the JAR archive
Returns:
the InputStream reference to the file data; null, if the process fails

closeInputStream

public static void closeInputStream(java.io.InputStream is)
Closes the given input stream.

Parameters:
is - the stream to close

closeOutputStream

public static void closeOutputStream(java.io.OutputStream os)
Closes the given output stream.

Parameters:
os - the stream to close

removeFiles

public static boolean removeFiles(java.io.File dirPath,
                                  boolean clean)
(Be very careful) Deletes all files in directory (but not the directory itself).

Parameters:
dirPath - path to a directory. A trailing file separator is ignored
clean - if true, all subdirectories are removed also; otherwise only files in dirPath are deleted and the subdirectories with their files are maintained
Returns:
true if all deletions were successful or dirPath does not exist; false, if a deletion fails or dirPath does not denote a directory

removeDir

public static boolean removeDir(java.io.File dirPath)
(Be very careful) Deletes all files and subdirectories and the directory itself.

Parameters:
dirPath - path to a directory. A trailing file separator is ignored
Returns:
true if all deletions were successful or dirPath does not exist; false, if a deletion fails or dirPath does not denote a directory

createDirectoryTree

public static boolean createDirectoryTree(java.io.File dirPath)
Creates a directory structure.

Parameters:
dirPath - a relative or absolute directory path that may contain subdirectories, e.g. /dir/subdir1/subdir2. A trailing file separator is ignored. Under Windows the absolute path may be proceeded by a drive designator (e.g. 'c:').
Returns:
true, if the directory creation was successful or the directory already exists; otherwise false

copyFile

public static boolean copyFile(java.net.URL url,
                               java.io.File dest)
Copies binary data from the source URL to the destination file. Creates the destination file, if it does not yet exist. An existing destination file is overwritten. The directory path of the destination file must exist.

Parameters:
url - the source URL, e.g. "http://myserver.com/image.gif"
dest - a relative or absolute file path of the destination file
Returns:
true, if the copy process was successful; otherwise false

copyFile

public static boolean copyFile(java.io.File src,
                               java.io.File dest)
Copies binary data from the source file to the destination file. Creates the destination file, if it does not yet exist. An existing destination file is overwritten. The directory path of the destination file must exist.

Parameters:
src - a relative or absolute file path of the source file
dest - a relative or absolute file path of the destination file
Returns:
true, if the copy process was successful; otherwise false

copyDirectory

public static boolean copyDirectory(java.io.File srcDir,
                                    java.io.File destDir,
                                    boolean collect)
Copies all files from the source directory into the destination directory. If the destination directory does not yet exist, it is created (with subdirectory structure). Existing files are overwritten.

Parameters:
srcDir - the source directory (absolute or relative)
destDir - the destination directory (absolute or relative)
collect - if true, all files in subdirectories of the source directory are copied to the root of the destination directory (use copyTree() to maintain the subdirectory structure under the destination directory)
Returns:
true, if the all files were successfully copied; otherwise false

copyTree

public static boolean copyTree(java.io.File srcDir,
                               java.io.File destDir)
Copies all files with subdirectory structure from the source directory to the destination directory. If the destination directory does not exist, it is created. Existing files are overwritten.

Parameters:
srcDir - the source directory (absolute or relative)
destDir - the destination directory (absolute or relative)
Returns:
true, if the all files were successfully copied; otherwise false

getNbFiles

public static int getNbFiles(java.io.File dirPath)
Returns number of files (subdirectories are not counted) in given directory.

Parameters:
dirPath - path to a directory. A trailing file separator is ignored
Returns:
the total number of files, 0 if dirPath is not a directory, does not exist or contains no files

isDirectoryEmpty

public static boolean isDirectoryEmpty(java.io.File dirPath)
Returns true, if the directory is empty (no files and no subdirectories).

Parameters:
dirPath - path to a directory. A trailing file separator is ignored
Returns:
true, if dirPath is not a directory, does not exist or is empty; otherwise false

unpack

public static boolean unpack(java.io.File jarFile,
                             java.io.File dirPath)
Unpacks the given JAR archive in the given directory. If the directory does not exist, the directory with its parent directories is created. (An empty JAR archive is illegal.)

Parameters:
jarFile - the JAR to unpack
dirPath - path to a directory. A trailing file separator is ignored
Returns:
true, if the JAR is successfully unpacked; otherwise false

pack

public static boolean pack(java.io.File jarFile,
                           java.io.File dirPath)
Creates a compressed JAR archive from a all files and subdirectories in directory with given dirPath.

Parameters:
jarFile - the JAR archive to create
dirPath - path to the directory that holds the files/subdirectories to add to the archive. An emtpy directory is illegal. A trailing file separator is ignored
Returns:
true, if successful; otherwise false

unzip

public static boolean unzip(java.io.File zipFile,
                            java.io.File dirPath)
Unpacks the given ZIP archive in the given directory. If the directory does not exist, the directory with its parent directories is created. (An empty ZIP archive is illegal.)

Parameters:
zipFile - the ZIP to unpack
dirPath - path to a directory. A trailing file separator is ignored
Returns:
true, if the ZIP is successfully unpacked; otherwise false

zip

public static boolean zip(java.io.File zipFile,
                          java.io.File dirPath)
Creates a ZIP archive from a all files and subdirectories in directory with given dirPath.

Parameters:
zipFile - the ZIP archive to create
dirPath - path to the directory that holds the files/subdirectories to add to the archive. An empty directory is illegal. A trailing file separator is ignored
Returns:
true, if successful; otherwise false