com.evanmclean.evlib.io
Class Files

java.lang.Object
  extended by com.evanmclean.evlib.io.Files

public final class Files
extends Object

Perform various file related operations. Also see Folders.

Author:
Evan McLean McLean Computer Services (see the overview for copyright and licensing.)

Method Summary
static void copy(File from, File to)
           Efficient file copy.
static void copy(File from, File to, boolean preserve_date)
           Efficient file copy.
static void del(File path)
          Deletes the file if it exists.
static boolean del(File path, boolean quietly)
          Deletes the file if it exists.
static boolean delhard(File path)
          Deletes the file if it exists, trying to make the file and/or the parent folder writable in order to do so.
static File getCanonicalFile(File path)
          Returns the canonical form of an abstract pathname.
static String getCanonicalPath(File path)
          Returns the canonical pathname string of an abstract pathname.
static boolean isEmpty(File path)
          Checks the path specified is a zero length file, an empty folder or does not exist.
static boolean isNotEmpty(File path)
          Checks the path specified is a zero length file, an empty folder or does not exist.
static void move(File from, File to)
           Efficient file move.
static void move(File from, File to, boolean preserve_date)
           Efficient file move.
static void rename(File from, File to)
          Renames a file or folder.
 
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 from,
                        File to)
                 throws IOException

Efficient file copy. The last modified date is preserved.

Uses the java.nio.channels logic to efficiently copy a file without having to create ancillary temporary buffers and such.

Parameters:
from - Source file to copy.
to - Destination file that will be overwitten.
Throws:
IOException

copy

public static void copy(File from,
                        File to,
                        boolean preserve_date)
                 throws IOException

Efficient file copy.

Uses the java.nio.channels logic to efficiently copy a file without having to create ancillary temporary buffers and such.

Parameters:
from - Source file to copy.
to - Destination file that will be overwritten.
preserve_date - If true then the last modified date is preserved.
Throws:
IOException

del

public static void del(File path)
                throws IOException
Deletes the file if it exists. Will also delete a folder if it is empty. See Folders.del(File) to delete a folder and its contents. This is basically the same as delhard(File) but throws an exception on error instead of returning false.

Parameters:
path - The file to delete.
Throws:
IOException - If unable to delete the file.

del

public static boolean del(File path,
                          boolean quietly)
                   throws IOException
Deletes the file if it exists. Will also delete a folder if it is empty. See Folders.del(File) to delete a folder and its contents. This is basically the same as delhard(File) but throws an exception on error instead of returning false.

Parameters:
path - The file to delete.
quietly - Only through an IO exception if quietly is false.
Returns:
True if the file was deleted.
Throws:
IOException - If unable to delete the file (and quietly is false).

delhard

public static boolean delhard(File path)
Deletes the file if it exists, trying to make the file and/or the parent folder writable in order to do so. Will also delete a folder if it is empty. See Folders.del(File) to delete a folder and its contents.

Parameters:
path - The file to delete.
Returns:
True if file was deleted.

getCanonicalFile

public static File getCanonicalFile(File path)
Returns the canonical form of an abstract pathname. Basically a wrapper around File.getCanonicalFile() which will throw a runtime exception on error instead of an IOException.

Parameters:
path -
Returns:
The canonical form of the abstract pathname. If path is null then null is returned.
Throws:
UnhandledException - Runtime wrapper around the IOException if it occurred.

getCanonicalPath

public static String getCanonicalPath(File path)
Returns the canonical pathname string of an abstract pathname. Basically a wrapper around File.getCanonicalPath() which will throw a runtime exception on error instead of an IOException.

Parameters:
path -
Returns:
The canonical pathname. If path is null then null is returned.
Throws:
UnhandledException - Runtime wrapper around the IOException if it occurred.

isEmpty

public static boolean isEmpty(File path)
                       throws IOException
Checks the path specified is a zero length file, an empty folder or does not exist.

Parameters:
path - The path the check.
Returns:
True if a zero length file, an empty folder or does not exist.
Throws:
IOException - If folder does not represent a file or a folder.

isNotEmpty

public static boolean isNotEmpty(File path)
                          throws IOException
Checks the path specified is a zero length file, an empty folder or does not exist.

Parameters:
path - The path the check.
Returns:
False if a zero length file, an empty folder or does not exist.
Throws:
IOException - If folder does not represent a file or a folder.

move

public static void move(File from,
                        File to)
                 throws IOException

Efficient file move. The last modified date is preserved.

Tries to do a rename, and if that fails calls copy(File, File, boolean) then del(File) to efficiently and effectively move the file.

Parameters:
from - Source file to move.
to - Destination file that will be overwritten.
Throws:
IOException

move

public static void move(File from,
                        File to,
                        boolean preserve_date)
                 throws IOException

Efficient file move.

Tries to do a rename, and if that fails calls copy(File, File, boolean) then del(File) to efficiently and effectively move the file.

Parameters:
from - Source file to move.
to - Destination file that will be overwritten.
preserve_date - If true then the last modified date is preserved.
Throws:
IOException

rename

public static void rename(File from,
                          File to)
                   throws IOException
Renames a file or folder. This is basically the same as File.renameTo(File) but throws an exception on error instead of returning false.

Parameters:
from - The file or folder to rename.
to - What we are renaming the file or folder to.
Throws:
IOException - If unable to rename the file or folder.