org.clapper.util.io
Class FileUtil

java.lang.Object
  extended by org.clapper.util.io.FileUtil

public class FileUtil
extends java.lang.Object

Static class containing miscellaneous file utility methods.

Version:
$Revision: 6735 $
Author:
Copyright © 2004-2007 Brian M. Clapper

Method Summary
static java.lang.String basename(java.io.File file)
          Get the base (i.e., simple file) name of a file.
static java.lang.String basename(java.lang.String fileName)
          Get the base (i.e., simple file) name of a file.
static int copyFile(java.io.File src, java.io.File dst)
          Copy one file to another.
static int copyReader(java.io.Reader reader, java.io.Writer writer)
          Copy characters from a reader to a writer, using a default buffer size.
static int copyReader(java.io.Reader reader, java.io.Writer writer, int bufferSize)
          Copy characters from a reader to a writer.
static int copyStream(java.io.InputStream is, java.io.OutputStream os)
          Copy an InputStream to an OutputStream.
static int copyStream(java.io.InputStream src, java.io.OutputStream dst, int bufferSize)
          Copy an InputStream to an OutputStream.
static int copyTextFile(java.io.File src, java.lang.String srcEncoding, java.io.File dst, java.lang.String dstEncoding)
          Copy one file to another, character by character, possibly doing character set conversions
static java.lang.String dirname(java.io.File file)
          Get the name of a file's parent directory.
static java.lang.String dirname(java.lang.String fileName)
          Get the name of a file's parent directory.
static java.lang.String getDefaultEncoding()
          Get the virtual machine's default encoding.
static java.lang.String getFileNameExtension(java.io.File file)
          Get the extension for a path or file name.
static java.lang.String getFileNameExtension(java.lang.String path)
          Get the extension for a path or file name.
static java.lang.String getFileNameNoExtension(java.io.File file)
          Get the name of a file without its extension.
static java.lang.String getFileNameNoExtension(java.lang.String path)
          Get the name of a file without its extension.
static boolean isAbsolutePath(java.lang.String path)
          Determine whether a string represents an absolute path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isAbsolutePath

public static boolean isAbsolutePath(java.lang.String path)
                              throws java.io.IOException
Determine whether a string represents an absolute path. On Unix, an absolute path must start with a "/". On Windows, it must begin with one of the machine's valid drive letters.

Parameters:
path - the path to check
Returns:
true if it's absolute, false if not
Throws:
java.io.IOException - on error

copyStream

public static int copyStream(java.io.InputStream is,
                             java.io.OutputStream os)
                      throws java.io.IOException
Copy an InputStream to an OutputStream. If either stream is not already buffered, then it's wrapped in the corresponding buffered stream (i.e., BufferedInputStream or BufferedOutputStream) before copying. Calling this method is equivalent to:
copyStream (src, dst, 8192);

Parameters:
is - the source InputStream
os - the destination OutputStream
Returns:
total number of bytes copied
Throws:
java.io.IOException - on error
See Also:
copyStream(InputStream,OutputStream,int), copyReader(Reader,Writer), copyFile(File,File)

copyStream

public static int copyStream(java.io.InputStream src,
                             java.io.OutputStream dst,
                             int bufferSize)
                      throws java.io.IOException
Copy an InputStream to an OutputStream. If either stream is not already buffered, then it's wrapped in the corresponding buffered stream (i.e., BufferedInputStream or BufferedOutputStream) before copying.

Parameters:
src - the source InputStream
dst - the destination OutputStream
bufferSize - the buffer size to use, or -1 for a default
Returns:
total number of bytes copied
Throws:
java.io.IOException - on error
See Also:
copyReader(Reader,Writer,int), copyStream(InputStream,OutputStream), copyFile(File,File)

copyReader

public static int copyReader(java.io.Reader reader,
                             java.io.Writer writer,
                             int bufferSize)
                      throws java.io.IOException
Copy characters from a reader to a writer. If the reader is not already buffered, then it's wrapped in a BufferedReader, using the specified buffer size. Similarly, buffered stream (i.e., BufferedInputStream or If the writer is not already buffered, then it's wrapped in a BufferedWriter, using the specified buffer size.

Parameters:
reader - where to read from
writer - where to write to
bufferSize - buffer size to use, if reader and writer are not already buffered, or -1 to use a default size.
Returns:
total number of characters copied
Throws:
java.io.IOException - on error
See Also:
copyReader(Reader,Writer), copyStream(InputStream,OutputStream,int), copyStream(InputStream,OutputStream), copyFile(File,File)

copyReader

public static int copyReader(java.io.Reader reader,
                             java.io.Writer writer)
                      throws java.io.IOException
Copy characters from a reader to a writer, using a default buffer size.

Parameters:
reader - where to read from
writer - where to write to
Returns:
total number of characters copied
Throws:
java.io.IOException - on error
See Also:
copyReader(Reader,Writer), copyStream(InputStream,OutputStream,int), copyStream(InputStream,OutputStream), copyFile(File,File)

copyFile

public static int copyFile(java.io.File src,
                           java.io.File dst)
                    throws java.io.IOException
Copy one file to another. This method simply copies bytes and performs no character set conversion. If you want character set conversions, use copyTextFile(File,String,File,String).

Parameters:
src - The file to copy
dst - Where to copy it. Can be a directory or a file.
Returns:
total number of bytes copied
Throws:
java.io.IOException - on error
See Also:
copyTextFile(File,String,File,String), copyReader(Reader,Writer,int), copyReader(Reader,Writer), copyStream(InputStream,OutputStream,int), copyStream(InputStream,OutputStream)

copyTextFile

public static int copyTextFile(java.io.File src,
                               java.lang.String srcEncoding,
                               java.io.File dst,
                               java.lang.String dstEncoding)
                        throws java.io.IOException
Copy one file to another, character by character, possibly doing character set conversions

Parameters:
src - the file to copy
srcEncoding - the character set encoding for the source file, or null to assume the default
dst - Where to copy it. Can be a directory or a file.
dstEncoding - the character set encoding for the destination file, or null to assume the default
Returns:
total number of characters copied
Throws:
java.io.IOException - on error
See Also:
copyFile(File,File), copyReader(Reader,Writer,int), copyReader(Reader,Writer), copyStream(InputStream,OutputStream,int), copyStream(InputStream,OutputStream)

getDefaultEncoding

public static java.lang.String getDefaultEncoding()
Get the virtual machine's default encoding.

Returns:
the default encoding

getFileNameExtension

public static java.lang.String getFileNameExtension(java.io.File file)
Get the extension for a path or file name. Does not include the ".".

Parameters:
file - the file
Returns:
the extension, or null if there isn't one

getFileNameExtension

public static java.lang.String getFileNameExtension(java.lang.String path)
Get the extension for a path or file name. Does not include the ".".

Parameters:
path - the file or path name
Returns:
the extension, or null if there isn't one

getFileNameNoExtension

public static java.lang.String getFileNameNoExtension(java.io.File file)
Get the name of a file without its extension. Does not remove any parent directory components. Uses File.getAbsolutePath(), not File.getCanonicalPath() to get the path.

Parameters:
file - the file
Returns:
the path without the extension

getFileNameNoExtension

public static java.lang.String getFileNameNoExtension(java.lang.String path)
Get the name of a file without its extension. Does not remove any parent directory components.

Parameters:
path - the path
Returns:
the path without the extension

dirname

public static java.lang.String dirname(java.lang.String fileName)
Get the name of a file's parent directory. This is the directory part of the filename. For instance, "/home/foo.zip" would return "/home". This method uses the file's absolute path.

Parameters:
fileName - the file name
Returns:
directory name part of the file's absolute pathname
See Also:
dirname(File), basename(String)

dirname

public static java.lang.String dirname(java.io.File file)
Get the name of a file's parent directory. This is the directory part of the filename. For instance, "/home/foo.zip" would return "/home". This method uses the file's absolute path.

Parameters:
file - the file whose parent directory is to be returned
Returns:
directory name part of the file's absolute pathname
See Also:
dirname(String), basename(File)

basename

public static java.lang.String basename(java.lang.String fileName)
Get the base (i.e., simple file) name of a file. This is the file name stripped of any directory information. For instance, "/home/foo.zip" would return "foo.zip".

Parameters:
fileName - name of the file to get the basename for
Returns:
file name part of the file
See Also:
dirname(String)

basename

public static java.lang.String basename(java.io.File file)
Get the base (i.e., simple file) name of a file. This is the file name stripped of any directory information. For instance, "/home/foo.zip" would return "foo.zip".

Parameters:
file - the file to get the basename for
Returns:
file name part of the file
See Also:
basename(String), dirname(File)


Copyright © 2004-2007 Brian M. Clapper. All Rights Reserved.