org.dbforms.util.external
Class FileUtil

java.lang.Object
  extended by org.dbforms.util.external.FileUtil

public class FileUtil
extends Object

DOCUMENT ME!

Version:
$Revision: 1.9 $
Author:
$author$

Field Summary
static int ONE_GB
          The number of bytes in a gigabyte.
static int ONE_KB
          The number of bytes in a kilobyte.
static int ONE_MB
          The number of bytes in a megabyte.
 
Method Summary
static String basename(String filename)
          Returns the filename portion of a file specification string.
static String basename(String filename, String suffix)
          Returns the filename portion of a file specification string.
static String byteCountToDisplaySize(int size)
          Returns a human-readable version of the file size (original is in bytes).
static String catPath(String lookupPath, String path)
          Will concatenate 2 paths.
static void cleanDirectory(File directory)
          Clean a directory without deleting it.
static void cleanDirectory(String directory)
          Clean a directory without deleting it.
static boolean contentEquals(File file1, File file2)
          Compare the contents of two files to determine if they are equal or not.
static void copyFile(File source, File destination)
          Copy file from source to destination.
static void copyFileToDirectory(File source, File destinationDirectory)
          Copy file from source to destination.
static void copyFileToDirectory(String source, String destinationDirectory)
          Copy file from source to destination.
static void copyURLToFile(URL source, File destination)
          Copies bytes from the URL source to a file destination.
static void deleteDirectory(File directory)
          Recursively delete a directory.
static void deleteDirectory(String directory)
          Recursively delete a directory.
static String dirname(String filename)
          Returns the directory path portion of a file specification string.
static String extension(String filename)
          Returns the extension portion of a file specification string.
static void fileCopy(String inFileName, String outFileName)
          Copy a file.
static void fileDelete(String fileName)
          Deletes a file.
static boolean fileExists(String fileName)
          Check if a file exits.
static String filename(String filename)
          Returns the filename portion of a file specification string.
static String fileRead(String fileName)
          Reads the contents of a file.
static void fileWrite(String fileName, String data)
          Writes data to a file.
static void forceDelete(File file)
          Delete a file.
static void forceDelete(String file)
          Delete a file.
static void forceDeleteOnExit(File file)
          Schedule a file to be deleted when JVM exits.
static void forceMkdir(File file)
          Make a directory.
static String getExtension(String filename)
          Get extension from filename. ie foo.txt --> "txt" a\b\c.jpg --> "jpg" a\b\c --> ""
static File getFile(String fileName)
          Creates a file handle.
static String[] getFilesFromExtension(String directory, String[] extensions)
          Given a directory and an array of extensions... return an array of compliant files.
static String getPath(String filepath)
          Get path from filename.
static String getPath(String filepath, char fileSeparatorChar)
          Get path from filename. ie.
static void mkdir(String dir)
          Simple way to make a directory
static String normalize(String path)
          Normalize a path.
static String removeExtension(String filename)
          Remove extension from filename. ie foo.txt --> foo a\b\c.jpg --> a\b\c a\b\c --> a\b\c
static String removePath(String filepath)
          Remove path from filename.
static String removePath(String filepath, char fileSeparatorChar)
          Remove path from filename. ie.
static File resolveFile(File baseFile, String filename)
          Resolve a file filename to it's canonical form.
static long sizeOfDirectory(File directory)
          Recursively count size of a directory.
static long sizeOfDirectory(String directory)
          Recursively count size of a directory.
static File toFile(URL url)
          Convert from a URL to a File.
static URL[] toURLs(File[] files)
          Convert the array of Files into a list of URLs.
static boolean waitFor(String fileName, int seconds)
          Waits for NFS to propagate a file creation, imposing a timeout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ONE_KB

public static final int ONE_KB
The number of bytes in a kilobyte.

See Also:
Constant Field Values

ONE_MB

public static final int ONE_MB
The number of bytes in a megabyte.

See Also:
Constant Field Values

ONE_GB

public static final int ONE_GB
The number of bytes in a gigabyte.

See Also:
Constant Field Values
Method Detail

getExtension

public static String getExtension(String filename)
Get extension from filename. ie
 foo.txt    --> "txt"
 a\b\c.jpg --> "jpg"
 a\b\c     --> ""
 

Parameters:
filename - the filename
Returns:
the extension of filename or "" if none

getFile

public static File getFile(String fileName)
Creates a file handle.

Parameters:
fileName - The name of the file.
Returns:
A File instance.

getFilesFromExtension

public static String[] getFilesFromExtension(String directory,
                                             String[] extensions)
Given a directory and an array of extensions... return an array of compliant files. The given extensions should be like "java" and not like ".java"

Parameters:
directory - DOCUMENT ME!
extensions - DOCUMENT ME!
Returns:
DOCUMENT ME!

getPath

public static String getPath(String filepath)
Get path from filename. Roughly equivalent to the unix command dirname. ie.
 a/b/c.txt --> a/b
 a.txt     --> ""
 

Parameters:
filepath - the filepath
Returns:
the filename minus path

getPath

public static String getPath(String filepath,
                             char fileSeparatorChar)
Get path from filename. ie.
 a/b/c.txt --> a/b
 a.txt     --> ""
 

Parameters:
filepath - the filepath
fileSeparatorChar - DOCUMENT ME!
Returns:
the filename minus path

basename

public static String basename(String filename)
Returns the filename portion of a file specification string. Matches the equally named unix command.

Parameters:
filename - DOCUMENT ME!
Returns:
The filename string without extension.

basename

public static String basename(String filename,
                              String suffix)
Returns the filename portion of a file specification string. Matches the equally named unix command.

Parameters:
filename - DOCUMENT ME!
suffix - DOCUMENT ME!
Returns:
DOCUMENT ME!

byteCountToDisplaySize

public static String byteCountToDisplaySize(int size)
Returns a human-readable version of the file size (original is in bytes).

Parameters:
size - The number of bytes.
Returns:
A human-readable display value (includes units).

catPath

public static String catPath(String lookupPath,
                             String path)
Will concatenate 2 paths. Paths with <code>..</code> will be properly handled. <p>Eg.,<br /> <code>/a/b/c</code> + <code>d</code> = <code>/a/b/d</code><br /> <code>/a/b/c</code> + <code>../d</code> = <code>/a/d</code><br /> </p> Thieved from Tomcat sources...

Parameters:
lookupPath - DOCUMENT ME!
path - DOCUMENT ME!
Returns:
The concatenated paths, or null if error occurs

cleanDirectory

public static void cleanDirectory(String directory)
                           throws IOException
Clean a directory without deleting it.

Parameters:
directory - DOCUMENT ME!
Throws:
IOException - DOCUMENT ME!

cleanDirectory

public static void cleanDirectory(File directory)
                           throws IOException
Clean a directory without deleting it.

Parameters:
directory - DOCUMENT ME!
Throws:
IOException - DOCUMENT ME!

contentEquals

public static boolean contentEquals(File file1,
                                    File file2)
                             throws IOException
Compare the contents of two files to determine if they are equal or not.

Parameters:
file1 - the first file
file2 - the second file
Returns:
true if the content of the files are equal or they both don't exist, false otherwise
Throws:
IOException - DOCUMENT ME!

copyFile

public static void copyFile(File source,
                            File destination)
                     throws IOException
Copy file from source to destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.

Parameters:
source - An existing non-directory File to copy bytes from.
destination - A non-directory File to write bytes to (possibly overwriting).
Throws:
IOException - if source does not exist, destination cannot be written to, or an IO error occurs during copying.

copyFileToDirectory

public static void copyFileToDirectory(String source,
                                       String destinationDirectory)
                                throws IOException
Copy file from source to destination. If destinationDirectory does not exist, it (and any parent directories) will be created. If a file source in destinationDirectory exists, it will be overwritten.

Parameters:
source - An existing File to copy.
destinationDirectory - A directory to copy source into.
Throws:
IOException - if source does not exist, the file in destinationDirectory cannot be written to, or an IO error occurs during copying.

copyFileToDirectory

public static void copyFileToDirectory(File source,
                                       File destinationDirectory)
                                throws IOException
Copy file from source to destination. If destinationDirectory does not exist, it (and any parent directories) will be created. If a file source in destinationDirectory exists, it will be overwritten.

Parameters:
source - An existing File to copy.
destinationDirectory - A directory to copy source into.
Throws:
IOException - if source does not exist, the file in destinationDirectory cannot be written to, or an IO error occurs during copying.

copyURLToFile

public static void copyURLToFile(URL source,
                                 File destination)
                          throws IOException
Copies bytes from the URL source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.

Parameters:
source - A URL to copy bytes from.
destination - A non-directory File to write bytes to (possibly overwriting).
Throws:
IOException - if
  • source URL cannot be opened
  • destination cannot be written to
  • an IO error occurs during copying

deleteDirectory

public static void deleteDirectory(String directory)
                            throws IOException
Recursively delete a directory.

Parameters:
directory - DOCUMENT ME!
Throws:
IOException - DOCUMENT ME!

deleteDirectory

public static void deleteDirectory(File directory)
                            throws IOException
Recursively delete a directory.

Parameters:
directory - DOCUMENT ME!
Throws:
IOException - DOCUMENT ME!

dirname

public static String dirname(String filename)
Returns the directory path portion of a file specification string. Matches the equally named unix command.

Parameters:
filename - DOCUMENT ME!
Returns:
The directory portion excluding the ending file separator.

extension

public static String extension(String filename)
Returns the extension portion of a file specification string. This everything after the last dot '.' in the filename (NOT including the dot).

Parameters:
filename - DOCUMENT ME!
Returns:
DOCUMENT ME!

fileCopy

public static void fileCopy(String inFileName,
                            String outFileName)
                     throws Exception
Copy a file. The new file will be created if it does not exist. This is an inefficient method, which just calls fileRead(String) and then fileWrite(String,String)

Parameters:
inFileName - the file to copy
outFileName - the file to copy to
Throws:
Exception - if fileRead or fileWrite throw it

fileDelete

public static void fileDelete(String fileName)
Deletes a file.

Parameters:
fileName - The name of the file to delete.

fileExists

public static boolean fileExists(String fileName)
Check if a file exits.

Parameters:
fileName - The name of the file to check.
Returns:
true if file exists.

fileRead

public static String fileRead(String fileName)
                       throws IOException
Reads the contents of a file.

Parameters:
fileName - The name of the file to read.
Returns:
The file contents or null if read failed.
Throws:
IOException - DOCUMENT ME!

fileWrite

public static void fileWrite(String fileName,
                             String data)
                      throws Exception
Writes data to a file. The file will be created if it does not exist.

Parameters:
fileName - The name of the file to write.
data - The content to write to the file.
Throws:
Exception - DOCUMENT ME!

filename

public static String filename(String filename)
Returns the filename portion of a file specification string.

Parameters:
filename - DOCUMENT ME!
Returns:
The filename string with extension.

forceDelete

public static void forceDelete(String file)
                        throws IOException
Delete a file. If file is directory delete it and all sub-directories.

Parameters:
file - DOCUMENT ME!
Throws:
IOException - DOCUMENT ME!

forceDelete

public static void forceDelete(File file)
                        throws IOException
Delete a file. If file is directory delete it and all sub-directories.

Parameters:
file - DOCUMENT ME!
Throws:
IOException - DOCUMENT ME!

forceDeleteOnExit

public static void forceDeleteOnExit(File file)
                              throws IOException
Schedule a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.

Parameters:
file - DOCUMENT ME!
Throws:
IOException - DOCUMENT ME!

forceMkdir

public static void forceMkdir(File file)
                       throws IOException
Make a directory. If there already exists a file with specified name or the directory is unable to be created then an exception is thrown.

Parameters:
file - DOCUMENT ME!
Throws:
IOException - DOCUMENT ME!

mkdir

public static void mkdir(String dir)
Simple way to make a directory

Parameters:
dir - DOCUMENT ME!

normalize

public static String normalize(String path)
Normalize a path. Eliminates "/../" and "/./" in a string. Returns null if the ..'s went past the root. Eg:
 /foo//               -->     /foo/
 /foo/./              -->     /foo/
 /foo/../bar          -->     /bar
 /foo/../bar/         -->     /bar/
 /foo/../bar/../baz   -->     /baz
 //foo//./bar         -->     /foo/bar
 /../                 -->     null
 

Parameters:
path - the path to normalize
Returns:
the normalized String, or null if too many ..'s.

removeExtension

public static String removeExtension(String filename)
Remove extension from filename. ie
 foo.txt    --> foo
 a\b\c.jpg --> a\b\c
 a\b\c     --> a\b\c
 

Parameters:
filename - the filename
Returns:
the filename minus extension

removePath

public static String removePath(String filepath)
Remove path from filename. Equivalent to the unix command basename ie.
 a/b/c.txt --> c.txt
 a.txt     --> a.txt
 

Parameters:
filepath - the filepath
Returns:
the filename minus path

removePath

public static String removePath(String filepath,
                                char fileSeparatorChar)
Remove path from filename. ie.
 a/b/c.txt --> c.txt
 a.txt     --> a.txt
 

Parameters:
filepath - the filepath
fileSeparatorChar - DOCUMENT ME!
Returns:
the filename minus path

resolveFile

public static File resolveFile(File baseFile,
                               String filename)
Resolve a file filename to it's canonical form. If filename is relative (doesn't start with /), it will be resolved relative to baseFile, otherwise it is treated as a normal root-relative path.

Parameters:
baseFile - Where to resolve filename from, if filename is relative.
filename - Absolute or relative file path to resolve.
Returns:
The canonical File of filename.

sizeOfDirectory

public static long sizeOfDirectory(String directory)
Recursively count size of a directory.

Parameters:
directory - DOCUMENT ME!
Returns:
size of directory in bytes.

sizeOfDirectory

public static long sizeOfDirectory(File directory)
Recursively count size of a directory.

Parameters:
directory - DOCUMENT ME!
Returns:
size of directory in bytes.

toFile

public static File toFile(URL url)
Convert from a URL to a File.

Parameters:
url - File URL.
Returns:
The equivalent File object, or null if the URL's protocol is not file

toURLs

public static URL[] toURLs(File[] files)
                    throws IOException
Convert the array of Files into a list of URLs.

Parameters:
files - the array of files
Returns:
the array of URLs
Throws:
IOException - if an error occurs

waitFor

public static boolean waitFor(String fileName,
                              int seconds)
Waits for NFS to propagate a file creation, imposing a timeout.

Parameters:
fileName - The name of the file.
seconds - The maximum time in seconds to wait.
Returns:
True if file exists.


Copyright © 2002-2006 DbForms. All Rights Reserved.