org.jcoderz.commons.util
Class FileUtils

java.lang.Object
  extended by org.jcoderz.commons.util.FileUtils

public final class FileUtils
extends Object

This class collects some nifty file utility functions. TODO: Cleanup check vs. IoUtil

Author:
Michael Griffel, Andreas Mandel

Method Summary
static void close(InputStream in)
          Deprecated. use IoUtil.close(InputStream)
static void close(OutputStream out)
          Deprecated. use IoUtil.close(OutputStream)
static void copy(File src, File destinationDir)
          Copies the file or directory src to the directory destinationDir.
static void copy(InputStream in, OutputStream out)
          Copy from an input stream to an output stream.
static void copyFile(File src, File dest)
          Copies the file src to the file or directory dest.
static void copySlashStar(File srcDir, File dst)
          Copy all files under srcDir to the directory dst.
static void createNewFile(File newFile)
          Creates the given file.
static File createTempDir(File baseDir, String prefix)
          Creates a temporary directory.
static void delete(File aFile)
          Deletes the given file aFile.
static List findFile(File path, String pattern)
          Search for files in a directory hierarchy.
static String getRelativePath(File baseDir, File file)
          Returns the relative path of file to the file basedir.
static void mkdir(File dir)
          Creates the given directory.
static void mkdirs(File dirs)
          Creates the given directories.
static void rename(File aFile, File dest)
          Renames the file aFile.
static void rmdir(File file)
          Remove file or directory.
static void safeClose(Reader reader)
          Deprecated. use IoUtil.close(Reader)
static void safeClose(Writer writer)
          Deprecated. use IoUtil.close(Writer)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

copy

public static void copy(File src,
                        File destinationDir)
                 throws IOException
Copies the file or directory src to the directory destinationDir.

Parameters:
src - the source file or directory.
destinationDir - the destination directory.
Throws:
IOException - in case of an I/O error.

copyFile

public static void copyFile(File src,
                            File dest)
                     throws FileNotFoundException,
                            IOException
Copies the file src to the file or directory dest.

Parameters:
src - The source file.
dest - The destination file or directory.
Throws:
FileNotFoundException - if the source file does not exists.
IOException - in case of an I/O error.

copy

public static void copy(InputStream in,
                        OutputStream out)
                 throws IOException
Copy from an input stream to an output stream.

Parameters:
in - The input stream.
out - The output stream.
Throws:
IOException - when an error happens during a read or a write operation.

copySlashStar

public static void copySlashStar(File srcDir,
                                 File dst)
                          throws IOException
Copy all files under srcDir to the directory dst. Unix command:
 $ cp "$scrDir/*" "$dst"
 

Parameters:
srcDir - the source directory.
dst - the destination directory.
Throws:
IOException - in case of an I/O error.

createTempDir

public static File createTempDir(File baseDir,
                                 String prefix)
                          throws IOException
Creates a temporary directory.

Parameters:
baseDir - the base directory.
prefix - prefix for the temporary directory.
Returns:
a temporary directory.
Throws:
IOException - in case of an I/O error.

close

public static void close(InputStream in)
Deprecated. use IoUtil.close(InputStream)

Closes the input stream (safe). This method tries to close the given input stream and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
in - the input stream that should be closed.

close

public static void close(OutputStream out)
Deprecated. use IoUtil.close(OutputStream)

Closes the output stream (safe). This method tries to close the given output stream and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
out - the output stream that should be closed.

safeClose

public static void safeClose(Reader reader)
Deprecated. use IoUtil.close(Reader)

Closes the reader (safe). This method tries to close the given reader and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
reader - the reader that should be closed.

safeClose

public static void safeClose(Writer writer)
Deprecated. use IoUtil.close(Writer)

Closes the writer (safe). This method tries to close the given writer and if an IOException occurs a message with the level Level.FINE is logged. It's safe to pass a null reference for the argument.

Parameters:
writer - the writer that should be closed.

findFile

public static List findFile(File path,
                            String pattern)
Search for files in a directory hierarchy. Unix command:
 find path -name pattern
 

Parameters:
path - root directory.
pattern - filename pattern.
Returns:
a list of files matching the given pattern under path.

rmdir

public static void rmdir(File file)
                  throws IOException
Remove file or directory. Unix command:
 rm -rf file
 

Parameters:
file - the file or directory to delete.
Throws:
IOException - in case of an I/O error.

getRelativePath

public static String getRelativePath(File baseDir,
                                     File file)
                              throws IOException
Returns the relative path of file to the file basedir.

Parameters:
baseDir - the base directory or file.
file - the file.
Returns:
the relative path of the file to the basedir.
Throws:
IOException - in case of an I/O error.

rename

public static void rename(File aFile,
                          File dest)
                   throws IOException
Renames the file aFile.

Parameters:
aFile - The file to be renamed.
dest - The new abstract pathname for the named file
Throws:
IOException - if the the renaming was not successful.

delete

public static void delete(File aFile)
                   throws IOException
Deletes the given file aFile.

Parameters:
aFile - The file to be deleted.
Throws:
IOException - if the the deletion was not successful.

mkdirs

public static void mkdirs(File dirs)
                   throws IOException
Creates the given directories.

Parameters:
dirs - the directories to be created.
Throws:
IOException - the directories could not be created.
See Also:
File.mkdirs()

mkdir

public static void mkdir(File dir)
                  throws IOException
Creates the given directory.

Parameters:
dir - the directory to be created.
Throws:
IOException - if the file could not be created.
See Also:
File.mkdir()

createNewFile

public static void createNewFile(File newFile)
                          throws IOException
Creates the given file.

Parameters:
newFile - the file to create
Throws:
IOException - the file could not be created.
See Also:
File.createNewFile()


Copyright 2007 The jCoderZ Project.