org.osesb.utilities.fileio
Class FileUtilities

java.lang.Object
  extended by org.osesb.utilities.fileio.FileUtilities

public final class FileUtilities
extends java.lang.Object

This Class contains utilities for handling files.

Since:
Version .9

Constructor Summary
FileUtilities()
           
 
Method Summary
static boolean cleanDirectory(java.io.File directory)
          Recursively cleans a directory without deleting it.
static boolean cleanDirectory(java.lang.String directoryName)
          Clean a directory without deleting it.
static boolean copy(java.io.File source, java.io.File destination)
           
static void copyFromURL(java.lang.String sourceURL, java.io.File destination)
           
static void copyStream(java.io.InputStream in, java.io.OutputStream out)
          Copy the data from the input stream to the output stream.
static void copyStream(java.io.InputStream in, java.io.OutputStream out, byte[] buffer)
          Copy the data from the input stream to the output stream, using the buffer.
static boolean deleteDirectory(java.io.File directory)
          Recursively deletes all files and subdirectories of directory and then deletes the directory.
static boolean deleteDirectory(java.lang.String directoryName)
          Recursively delete a directory.
static boolean deleteFile(java.io.File file)
          Deletes a file.
static boolean deleteFile(java.lang.String filePath)
          Deletes s file.
static java.util.List findFiles(java.io.File directory)
          Find File objects in the given directory and all subdirectories.
static java.util.List findFiles(java.io.File directory, java.io.FileFilter fileFilter)
          Uses the given fileFilter to find File objects in the given directory and all subdirectories.
static java.util.List findFiles(java.io.File directory, java.lang.String pattern)
          Find File objects in the given directory and all subdirectories that meet the specified pattern (e.g.
static java.lang.String getExtension(java.io.File theFile)
          Returns the extension part of the files name.
static java.lang.String getExtension(java.lang.String filename)
          Returns the extension from a string.
static java.lang.String getFileName(java.lang.String filepath)
          Return the filename from path.
static java.lang.String getFileName(java.lang.String filepath, char fileSeparatorChar)
          Return the filename from path.
static java.lang.String getPath(java.lang.String filepath)
          Return the path from filename.
static java.lang.String getPath(java.lang.String filepath, char fileSeparatorChar)
          Return the path from filename.
static java.io.File getResourcePathAsFile(java.lang.String resourcePath)
          Returns a File object for a resource path, if the resource path can be resolved to a file system path.
static java.io.File getURLAsFile(java.lang.String urlString)
          Creates a File from a file system path given as a URL.
static java.lang.String javaFileName(java.lang.String filename)
          Standardize a files name.
static java.lang.String[] list(java.io.File dir)
          Lists the directory listing.
static void main(java.lang.String[] args)
          main(): For testing and sandboxing.
static java.io.File mkdirFromPath(java.lang.String directoryPath)
          Creates a directory using the given path.
static java.io.File mkdirFromURL(java.lang.String directoryURL)
          Creates a directory using the given URL.
static boolean move(java.io.File from, java.io.File to)
          Move a file from one location to another.
static boolean move(java.io.File from, java.io.File to, boolean overwrite)
          Move a file from one location to another.
static byte[] readBytes(java.io.File theFile)
          Reads the file into a byte[].
static byte[] readBytes(java.io.InputStream istream)
          Reads the InputStream into a byte[].
static byte[] readBytes(java.io.InputStream istream, boolean closeStream)
          Reads the InputStream into a byte[] and optionally closes the InputStream.
static java.lang.String readFileURLAsTextFile(java.lang.String urlString)
          Reads the text file from a file system path given as a URL.
static java.io.Serializable readObj(java.io.File theFile)
          Reads a serialized Object from a File.
static java.lang.String readPathAsTextFile(java.lang.String path)
          Reads the text file from a file system path.
static java.lang.String readText(java.io.File theFile)
          Reads the file assuming that it is a text file.
static void writeBytes(byte[] b, java.io.File theFile)
          Writes the contents of a byte-array to the File.
static void writeObj(java.io.Serializable object, java.io.File theFile)
          Writes and Object to the file in serialized format.
static void writeStream(java.io.InputStream istream, java.io.File theFile)
          Writes an InputStream to the File.
static void writeText(java.lang.String text, java.io.File theFile)
          Writes a String to the File.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtilities

public FileUtilities()
Method Detail

writeText

public static void writeText(java.lang.String text,
                             java.io.File theFile)
                      throws java.io.IOException,
                             java.lang.Exception
Writes a String to the File.

Throws:
java.io.IOException
java.lang.Exception

writeBytes

public static void writeBytes(byte[] b,
                              java.io.File theFile)
                       throws java.io.IOException,
                              java.lang.Exception
Writes the contents of a byte-array to the File.

Throws:
java.io.IOException
java.lang.Exception

writeStream

public static void writeStream(java.io.InputStream istream,
                               java.io.File theFile)
                        throws java.io.IOException,
                               java.lang.Exception
Writes an InputStream to the File.

Throws:
java.io.IOException
java.lang.Exception

readBytes

public static byte[] readBytes(java.io.File theFile)
                        throws java.io.IOException,
                               java.lang.Exception
Reads the file into a byte[].

Throws:
java.io.IOException
java.lang.Exception

readBytes

public static byte[] readBytes(java.io.InputStream istream)
                        throws java.io.IOException,
                               java.lang.Exception
Reads the InputStream into a byte[]. Note that this method closes the InputStream, so it will not be available for re-reading. If the InputStream will be re-read, then use readBytes(istream, false).

Throws:
java.io.IOException
java.lang.Exception

readBytes

public static byte[] readBytes(java.io.InputStream istream,
                               boolean closeStream)
                        throws java.io.IOException,
                               java.lang.Exception
Reads the InputStream into a byte[] and optionally closes the InputStream.

Throws:
java.io.IOException
java.lang.Exception

readPathAsTextFile

public static java.lang.String readPathAsTextFile(java.lang.String path)
                                           throws java.lang.Exception
Reads the text file from a file system path.

Throws:
java.lang.Exception

readFileURLAsTextFile

public static java.lang.String readFileURLAsTextFile(java.lang.String urlString)
                                              throws java.lang.Exception
Reads the text file from a file system path given as a URL.

Throws:
java.lang.Exception

getURLAsFile

public static java.io.File getURLAsFile(java.lang.String urlString)
                                 throws java.lang.Exception
Creates a File from a file system path given as a URL.

Throws:
java.lang.Exception

getResourcePathAsFile

public static java.io.File getResourcePathAsFile(java.lang.String resourcePath)
                                          throws java.lang.Exception
Returns a File object for a resource path, if the resource path can be resolved to a file system path. The resource path is given in the format for ClassLoader.getResource().

Throws:
java.lang.Exception

readText

public static java.lang.String readText(java.io.File theFile)
                                 throws java.io.IOException,
                                        java.lang.Exception
Reads the file assuming that it is a text file. If not, it will return gibberish.

Throws:
java.io.IOException
java.lang.Exception

copyFromURL

public static final void copyFromURL(java.lang.String sourceURL,
                                     java.io.File destination)
                              throws java.io.IOException,
                                     java.lang.Exception
Throws:
java.io.IOException
java.lang.Exception

copy

public static final boolean copy(java.io.File source,
                                 java.io.File destination)
                          throws java.io.IOException,
                                 java.lang.Exception
Throws:
java.io.IOException
java.lang.Exception

copyStream

public static void copyStream(java.io.InputStream in,
                              java.io.OutputStream out)
                       throws java.io.IOException,
                              java.lang.Exception
Copy the data from the input stream to the output stream.

Throws:
java.io.IOException
java.lang.Exception

copyStream

public static void copyStream(java.io.InputStream in,
                              java.io.OutputStream out,
                              byte[] buffer)
                       throws java.io.IOException,
                              java.lang.Exception
Copy the data from the input stream to the output stream, using the buffer.

Throws:
java.io.IOException
java.lang.Exception

writeObj

public static void writeObj(java.io.Serializable object,
                            java.io.File theFile)
                     throws java.io.IOException,
                            java.lang.Exception
Writes and Object to the file in serialized format.

Throws:
java.io.IOException
java.lang.Exception

readObj

public static java.io.Serializable readObj(java.io.File theFile)
                                    throws java.io.IOException,
                                           java.lang.ClassNotFoundException,
                                           java.lang.Exception
Reads a serialized Object from a File.

Throws:
java.lang.ClassNotFoundException - if the class saved in this file is not described somewhere in the CLASSPATH.
java.io.IOException
java.lang.Exception

deleteFile

public static boolean deleteFile(java.lang.String filePath)
                          throws java.io.IOException,
                                 java.lang.Exception
Deletes s file.

Throws:
java.io.IOException
java.lang.Exception

deleteFile

public static boolean deleteFile(java.io.File file)
                          throws java.io.IOException,
                                 java.lang.Exception
Deletes a file.

Throws:
java.io.IOException
java.lang.Exception

deleteDirectory

public static boolean deleteDirectory(java.lang.String directoryName)
                               throws java.io.IOException,
                                      java.lang.Exception
Recursively delete a directory.

Throws:
java.io.IOException
java.lang.Exception

deleteDirectory

public static boolean deleteDirectory(java.io.File directory)
                               throws java.io.IOException,
                                      java.lang.Exception
Recursively deletes all files and subdirectories of directory and then deletes the directory.

Throws:
java.io.IOException
java.lang.Exception

cleanDirectory

public static boolean cleanDirectory(java.lang.String directoryName)
                              throws java.io.IOException,
                                     java.lang.Exception
Clean a directory without deleting it.

Throws:
java.io.IOException
java.lang.Exception

cleanDirectory

public static boolean cleanDirectory(java.io.File directory)
                              throws java.io.IOException,
                                     java.lang.Exception
Recursively cleans a directory without deleting it.

Throws:
java.io.IOException
java.lang.Exception

move

public static boolean move(java.io.File from,
                           java.io.File to)
                    throws java.io.IOException,
                           java.lang.Exception
Move a file from one location to another. An attempt is made to rename the file and if that fails, the file is copied and the old file deleted.

If the destination file already exists, and oberwrite is false an exception is thrown.

Parameters:
from - file which should be moved.
to - desired destination of the file.
Throws:
java.io.IOException - if an error occurs.
java.lang.Exception

move

public static boolean move(java.io.File from,
                           java.io.File to,
                           boolean overwrite)
                    throws java.io.IOException,
                           java.lang.Exception
Move a file from one location to another. An attempt is made to rename the file and if that fails, the file is copied and the old file deleted.

Throws:
java.io.IOException
java.lang.Exception

mkdirFromPath

public static java.io.File mkdirFromPath(java.lang.String directoryPath)
                                  throws java.io.IOException,
                                         java.lang.Exception
Creates a directory using the given path.

Throws:
java.io.IOException
java.lang.Exception

mkdirFromURL

public static java.io.File mkdirFromURL(java.lang.String directoryURL)
                                 throws java.io.IOException,
                                        java.lang.Exception
Creates a directory using the given URL.

Throws:
java.io.IOException
java.lang.Exception

list

public static java.lang.String[] list(java.io.File dir)
                               throws java.io.IOException,
                                      java.lang.Exception
Lists the directory listing. Returns null if an error occurs.

Throws:
java.io.IOException
java.lang.Exception

getPath

public static java.lang.String getPath(java.lang.String filepath)
Return the path from filename.


getPath

public static java.lang.String getPath(java.lang.String filepath,
                                       char fileSeparatorChar)
Return the path from filename.


getFileName

public static java.lang.String getFileName(java.lang.String filepath)
Return the filename from path.


getFileName

public static java.lang.String getFileName(java.lang.String filepath,
                                           char fileSeparatorChar)
Return the filename from path.


getExtension

public static java.lang.String getExtension(java.lang.String filename)
Returns the extension from a string. For example getExtension(filename.txt) returns the string "txt".


getExtension

public static java.lang.String getExtension(java.io.File theFile)
Returns the extension part of the files name.


javaFileName

public static java.lang.String javaFileName(java.lang.String filename)
Standardize a files name. Convert back slashes to forward slashes. Drive letter to lower case.


findFiles

public static java.util.List findFiles(java.io.File directory,
                                       java.lang.String pattern)
                                throws java.lang.Exception
Find File objects in the given directory and all subdirectories that meet the specified pattern (e.g. *.xml)

Throws:
java.lang.Exception

findFiles

public static java.util.List findFiles(java.io.File directory)
                                throws java.lang.Exception
Find File objects in the given directory and all subdirectories.

Throws:
java.lang.Exception

findFiles

public static java.util.List findFiles(java.io.File directory,
                                       java.io.FileFilter fileFilter)
                                throws java.lang.Exception
Uses the given fileFilter to find File objects in the given directory and all subdirectories.

Throws:
java.lang.Exception

main

public static void main(java.lang.String[] args)
main(): For testing and sandboxing.

Parameters:
args - commandline arguments