maspack.fileutil
Class SafeFileUtils

java.lang.Object
  extended by maspack.fileutil.SafeFileUtils

public class SafeFileUtils
extends java.lang.Object


Field Summary
static int CLEAN_LOCK
           
static int DEFAULT_OPTIONS
           
static int LOCK_FILE
           
static long ONE_KB
           
static long ONE_MB
           
static int PRESERVE_DATE
           
 
Constructor Summary
SafeFileUtils()
           
 
Method Summary
static boolean closeQuietly(java.nio.channels.FileChannel stream)
           
static boolean closeQuietly(java.io.InputStream stream)
           
static boolean closeQuietly(java.io.OutputStream stream)
           
static void copyFile(java.io.File srcFile, java.io.File destFile)
          Copies a file to a new location preserving the file date.
static void copyFile(java.io.File srcFile, java.io.File destFile, int options)
          Copies a file to a new location.
static boolean deleteQuietly(java.io.File file)
          Deletes a file, never throwing an exception.
static void moveFile(java.io.File srcFile, java.io.File destFile)
           
static void moveFile(java.io.File srcFile, java.io.File destFile, int options)
          Moves a file.
static void setDefaultOptions(int options)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ONE_KB

public static final long ONE_KB
See Also:
Constant Field Values

ONE_MB

public static final long ONE_MB
See Also:
Constant Field Values

LOCK_FILE

public static final int LOCK_FILE
See Also:
Constant Field Values

CLEAN_LOCK

public static final int CLEAN_LOCK
See Also:
Constant Field Values

PRESERVE_DATE

public static final int PRESERVE_DATE
See Also:
Constant Field Values

DEFAULT_OPTIONS

public static int DEFAULT_OPTIONS
Constructor Detail

SafeFileUtils

public SafeFileUtils()
Method Detail

moveFile

public static void moveFile(java.io.File srcFile,
                            java.io.File destFile,
                            int options)
                     throws java.io.IOException
Moves a file.

When the destination file is on another file system, do a "copy and delete".

Parameters:
srcFile - the file to be moved
destFile - the destination file
Throws:
java.lang.NullPointerException - if source or destination is null
FileExistsException - if the destination file exists
java.io.IOException - if source or destination is invalid
java.io.IOException - if an IO error occurs moving the file
Since:
1.4

moveFile

public static void moveFile(java.io.File srcFile,
                            java.io.File destFile)
                     throws java.io.IOException
Throws:
java.io.IOException

deleteQuietly

public static boolean deleteQuietly(java.io.File file)
Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.

The difference between File.delete() and this method are:

Parameters:
file - file or directory to delete, can be null
Returns:
true if the file or directory was deleted, otherwise false
Since:
1.4

copyFile

public static void copyFile(java.io.File srcFile,
                            java.io.File destFile)
                     throws java.io.IOException
Copies a file to a new location preserving the file date.

This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

Note: This method tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

Parameters:
srcFile - an existing file to copy, must not be null
destFile - the new file, must not be null
Throws:
java.lang.NullPointerException - if source or destination is null
java.io.IOException - if source or destination is invalid
java.io.IOException - if an IO error occurs during copying

copyFile

public static void copyFile(java.io.File srcFile,
                            java.io.File destFile,
                            int options)
                     throws java.io.IOException
Copies a file to a new location.

This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

Note: Setting preserveFileDate to true tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

Parameters:
srcFile - an existing file to copy, must not be null
destFile - the new file, must not be null
options - determine whether to use file locks and preserve date information
Throws:
java.lang.NullPointerException - if source or destination is null
java.io.IOException - if source or destination is invalid
java.io.IOException - if an IO error occurs during copying

closeQuietly

public static boolean closeQuietly(java.io.InputStream stream)

closeQuietly

public static boolean closeQuietly(java.io.OutputStream stream)

closeQuietly

public static boolean closeQuietly(java.nio.channels.FileChannel stream)

setDefaultOptions

public static void setDefaultOptions(int options)