net.atlanticbb.tantlinger.io
Class IOUtils

java.lang.Object
  extended by net.atlanticbb.tantlinger.io.IOUtils

public class IOUtils
extends java.lang.Object

Various IO utility methods

Author:
Bob Tantlinger

Constructor Summary
IOUtils()
           
 
Method Summary
static void close(java.io.InputStream c)
          Closes a Closeable, swallowing any exceptions
static void close(java.io.OutputStream c)
           
static void close(java.io.Reader c)
           
static void close(java.io.Writer c)
           
static void copy(java.io.File src, java.io.File dst)
          Copies a file.
static void copy(java.io.File src, java.io.File dst, boolean overwrite)
          Copies a file.
static void copy(java.io.File src, java.io.File dst, FileCopyMonitor mon)
          Copies a file.
static void copy(java.io.File src, java.io.File dst, FileCopyMonitor mon, boolean overwrite)
          Copies a file
static void copy(java.io.InputStream src, java.io.OutputStream dst)
          Copies an InputStream to an OutputStream
static void copy(java.io.InputStream src, java.io.OutputStream dst, CopyMonitor mon)
          Copies an InputStream to an OutputStream
static void copy(java.io.Reader src, java.io.Writer dst)
          Copies a Reader to a Writer.
static void copy(java.lang.String srcPath, java.lang.String dstPath)
          Copies the file at the specified source path to the destination path.
static void copy(java.lang.String srcPath, java.lang.String dstPath, boolean overwrite)
          Copies the file at the specified source path to the destination path.
static void copy(java.lang.String srcPath, java.lang.String dstPath, FileCopyMonitor mon)
          Copies the file at the specified source path to the destination path.
static void copy(java.lang.String srcPath, java.lang.String dstPath, FileCopyMonitor mon, boolean overwrite)
          Copies the file at the specified source path to the destination path.
static void copyFiles(java.io.File src, java.io.File dest)
           
static void copyFiles(java.io.File src, java.io.File dest, FileCopyMonitor mon)
          Recursively copy all files from one directory to another.
static java.io.File createUniqueFile(java.io.File f)
          If the specified file already exists, a new, uniquely named file is returned.
static boolean deleteRecursively(java.io.File file)
          Recursively deletes a directory, thereby removing all its contents
static java.io.File[] getDirectoryContents(java.io.File rootdir)
           
static java.lang.String getExtension(java.io.File f)
          Gets the extension of a file e.g anything after the '.'
static java.lang.String getName(java.io.File f)
          Gets the name of the file without the extension
static long getTotalBytes(java.io.File file)
          Gets the total bytes in the file or directory
static java.lang.String read(java.io.File file)
          Reads a File and returns the contents as a String
static java.lang.String read(java.io.InputStream input)
           
static java.lang.String read(java.io.Reader input)
          Reads a String from a Reader
static java.lang.String sanitize(java.lang.String x)
           
static void write(java.io.File file, java.io.InputStream input)
          Writes the raw data from an InputStream to a File
static void write(java.io.File file, java.lang.String str)
          Writes a String to a File using a PrintWriter
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOUtils

public IOUtils()
Method Detail

getDirectoryContents

public static java.io.File[] getDirectoryContents(java.io.File rootdir)

sanitize

public static java.lang.String sanitize(java.lang.String x)

getExtension

public static java.lang.String getExtension(java.io.File f)
Gets the extension of a file e.g anything after the '.'

Parameters:
f - The file from which to get the extension
Returns:
The extension, or an empty string if the file has no extension

getName

public static java.lang.String getName(java.io.File f)
Gets the name of the file without the extension

Parameters:
f - The file
Returns:
The name, sans any extension

createUniqueFile

public static java.io.File createUniqueFile(java.io.File f)
If the specified file already exists, a new, uniquely named file is returned. It's name is incremented. For example if a file named "file.txt" already exists, a file named "file-1.txt" is returned.

Parameters:
f -
Returns:

copy

public static void copy(java.io.Reader src,
                        java.io.Writer dst)
                 throws java.io.IOException
Copies a Reader to a Writer.

Parameters:
src -
dst -
Throws:
java.io.IOException

copy

public static void copy(java.io.InputStream src,
                        java.io.OutputStream dst)
                 throws java.io.IOException
Copies an InputStream to an OutputStream

Parameters:
src -
dst -
Throws:
java.io.IOException

copy

public static void copy(java.io.InputStream src,
                        java.io.OutputStream dst,
                        CopyMonitor mon)
                 throws java.io.IOException
Copies an InputStream to an OutputStream

Parameters:
src -
dst -
mon - monitors the bytes copied and if the copy was aborted
Throws:
java.io.IOException

copy

public static void copy(java.io.File src,
                        java.io.File dst)
                 throws java.io.FileNotFoundException,
                        java.io.IOException
Copies a file. Overwrites the destination file if it already exists

Parameters:
src -
dst -
Throws:
java.io.FileNotFoundException
java.io.IOException

copy

public static void copy(java.io.File src,
                        java.io.File dst,
                        boolean overwrite)
                 throws java.io.FileNotFoundException,
                        java.io.IOException
Copies a file.

Parameters:
src -
dst -
overwrite - If true overwrites the dest file if it exists
Throws:
java.io.FileNotFoundException
java.io.IOException

copy

public static void copy(java.io.File src,
                        java.io.File dst,
                        FileCopyMonitor mon)
                 throws java.io.FileNotFoundException,
                        java.io.IOException
Copies a file. Overwrites the destination file if it already exists

Parameters:
src -
dst -
mon - monitors the copy
Throws:
java.io.FileNotFoundException
java.io.IOException

copy

public static void copy(java.io.File src,
                        java.io.File dst,
                        FileCopyMonitor mon,
                        boolean overwrite)
                 throws java.io.FileNotFoundException,
                        java.io.IOException
Copies a file

Parameters:
src - The source file
dst - The destination file
mon - Monitors the copy
overwrite - if true, overwrites a destination file with the same name otherwise, it creates a new destination file with a unique name before the copy
Throws:
java.io.FileNotFoundException
java.io.IOException

copy

public static void copy(java.lang.String srcPath,
                        java.lang.String dstPath)
                 throws java.io.FileNotFoundException,
                        java.io.IOException
Copies the file at the specified source path to the destination path. Overwrites the destination file if it already exists.

Parameters:
srcPath -
dstPath -
Throws:
java.io.FileNotFoundException
java.io.IOException

copy

public static void copy(java.lang.String srcPath,
                        java.lang.String dstPath,
                        FileCopyMonitor mon)
                 throws java.io.FileNotFoundException,
                        java.io.IOException
Copies the file at the specified source path to the destination path. Overwrites the destination file if it already exists.

Parameters:
srcPath -
mon - Monitors the copy
dstPath -
Throws:
java.io.FileNotFoundException
java.io.IOException

copy

public static void copy(java.lang.String srcPath,
                        java.lang.String dstPath,
                        boolean overwrite)
                 throws java.io.FileNotFoundException,
                        java.io.IOException
Copies the file at the specified source path to the destination path.

Parameters:
srcPath -
dstPath -
overwrite - If true overwrites the file at the dest path if it exists
Throws:
java.io.FileNotFoundException
java.io.IOException

copy

public static void copy(java.lang.String srcPath,
                        java.lang.String dstPath,
                        FileCopyMonitor mon,
                        boolean overwrite)
                 throws java.io.FileNotFoundException,
                        java.io.IOException
Copies the file at the specified source path to the destination path.

Parameters:
srcPath -
dstPath -
mon - Monitors the copy
overwrite - If true overwrites the file at the dest path if it exists
Throws:
java.io.FileNotFoundException
java.io.IOException

copyFiles

public static void copyFiles(java.io.File src,
                             java.io.File dest)
                      throws java.io.IOException,
                             java.io.FileNotFoundException
Throws:
java.io.IOException
java.io.FileNotFoundException

copyFiles

public static void copyFiles(java.io.File src,
                             java.io.File dest,
                             FileCopyMonitor mon)
                      throws java.io.FileNotFoundException,
                             java.io.IOException
Recursively copy all files from one directory to another.

Parameters:
src - File or directory to copy from.
dest - File or directory to copy to.
Throws:
java.io.IOException
java.io.FileNotFoundException
java.io.IOException

getTotalBytes

public static long getTotalBytes(java.io.File file)
Gets the total bytes in the file or directory

Parameters:
file - A File or directoy.
Returns:
The total bytes of the File. If the File is a directory the total bytes of all files in all subfolders is returned

read

public static java.lang.String read(java.io.InputStream input)
                             throws java.io.IOException
Throws:
java.io.IOException

read

public static java.lang.String read(java.io.File file)
                             throws java.io.FileNotFoundException,
                                    java.io.IOException
Reads a File and returns the contents as a String

Parameters:
file - The File to read
Returns:
The contents of the file
Throws:
java.io.FileNotFoundException
java.io.IOException

read

public static java.lang.String read(java.io.Reader input)
                             throws java.io.IOException
Reads a String from a Reader

Parameters:
input -
Returns:
Throws:
java.io.IOException

write

public static void write(java.io.File file,
                         java.lang.String str)
                  throws java.io.FileNotFoundException,
                         java.io.IOException
Writes a String to a File using a PrintWriter

Parameters:
file -
str -
Throws:
java.io.FileNotFoundException
java.io.IOException

write

public static void write(java.io.File file,
                         java.io.InputStream input)
                  throws java.io.FileNotFoundException,
                         java.io.IOException
Writes the raw data from an InputStream to a File

Parameters:
file -
input -
Throws:
java.io.FileNotFoundException
java.io.IOException

deleteRecursively

public static boolean deleteRecursively(java.io.File file)
Recursively deletes a directory, thereby removing all its contents

Parameters:
file - the file or file to delete
Returns:
true if the file(s) were successfully deleted

close

public static void close(java.io.InputStream c)
Closes a Closeable, swallowing any exceptions

Parameters:
out -

close

public static void close(java.io.OutputStream c)

close

public static void close(java.io.Reader c)

close

public static void close(java.io.Writer c)