net.sourceforge.java.util.io
Class FileUtils

java.lang.Object
  extended bynet.sourceforge.java.util.io.FileUtils

public class FileUtils
extends Object

Contains File related utility methods.

Author:
Sebastian Machhausen

Nested Class Summary
static class FileUtils.FileComparator
           Abstract base Comparator for file attributes.
private static class FileUtils.FileDateComparator
           Comparator to compare File objects based on their file modification date attributes.
private static class FileUtils.FileNameComparator
           Comparator to compare File objects based on their file name attributes.
private static class FileUtils.FileSizeComparator
           Comparator to compare File objects by their size attribute.
 
Field Summary
static FileUtils.FileDateComparator FILE_DATE_COMPARATOR
           The default Comparator to compare File objects based on their file modification date attributes.
static FileUtils.FileNameComparator FILE_NAME_COMPARATOR
           The default Comparator to compare File objects based on their file name attributes.
static String FILE_SEPARATOR
           The default operating system file separator char
static FileUtils.FileSizeComparator FILE_SIZE_COMPARATOR
           The default Comparator to compare File objects based on their file size attributes.
private static int KILOBYTE
           The number of bytes of a kilobyte
 
Constructor Summary
FileUtils()
           
 
Method Summary
static String addFileExtension(String fileName, String extension)
           Adds the specified file extension to the specified fileName in case no extension has been set.
static boolean containsFile(File directory, File f, boolean recursive)
           Returns true if the specified directory contains the specified File.
static void copyFile(File sourceFile, File targetFile)
           Copies the File denoted by sourcFile to the specified targetFile.
static void deleteRecursive(File file)
           Deletes the specified directory recursively.
static String getFileExtension(File f)
           Gets the extension of the specified File.
static String getFileExtension(String filename)
           Gets the extension of the File denoted by the specified filename.
static double getSize(File file)
           Gets the size of the specified File.
static double getSize(String filename)
           Gets the size of the File spedified by the given filename.
static boolean isReadable(File f)
           Tests if the File denoted by f is readable, i.e. read access is guaranteed.
static boolean isWriteable(File f)
           Tests if the File denoted by f is writeable, i.e. write access is guaranteed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_SEPARATOR

public static final String FILE_SEPARATOR

The default operating system file separator char


FILE_NAME_COMPARATOR

public static final FileUtils.FileNameComparator FILE_NAME_COMPARATOR

The default Comparator to compare File objects based on their file name attributes.


FILE_SIZE_COMPARATOR

public static final FileUtils.FileSizeComparator FILE_SIZE_COMPARATOR

The default Comparator to compare File objects based on their file size attributes.


FILE_DATE_COMPARATOR

public static final FileUtils.FileDateComparator FILE_DATE_COMPARATOR

The default Comparator to compare File objects based on their file modification date attributes.


KILOBYTE

private static final int KILOBYTE

The number of bytes of a kilobyte

See Also:
Constant Field Values
Constructor Detail

FileUtils

public FileUtils()
Method Detail

addFileExtension

public static String addFileExtension(String fileName,
                                      String extension)

Adds the specified file extension to the specified fileName in case no extension has been set.

Parameters:
fileName - the file name to add the specified file extension to
extension - the extension to add (without the . character, e.g. xml)
Returns:
the updated fileName

getFileExtension

public static String getFileExtension(File f)

Gets the extension of the specified File.

Parameters:
f - the File to get the extension for
Returns:
the extension of the File or an empty String if the File has no extension

getFileExtension

public static String getFileExtension(String filename)

Gets the extension of the File denoted by the specified filename.

Parameters:
filename - the name of the File to get the extension for
Returns:
the extension of the File denoted by the specified filename or an empty String if no extension set in the specified filename

deleteRecursive

public static void deleteRecursive(File file)

Deletes the specified directory recursively. If the specified File is not a directory it is deleted directly.

Parameters:
file - the directory or file to delete

getSize

public static double getSize(String filename)

Gets the size of the File spedified by the given filename. If filename denotes a directory the size is determined for the directory itself and recursively for all of its sub directories.

Parameters:
filename - the name of the File to get the size for
Returns:
the size in kilobytes (1k = 1024 bytes)

getSize

public static double getSize(File file)

Gets the size of the specified File. If file denotes a directory the size is determined for the directory itself and recursively for all of its sub directories.

Parameters:
file - the File to get the size for
Returns:
the size in kilobytes (1k = 1024 bytes)

containsFile

public static boolean containsFile(File directory,
                                   File f,
                                   boolean recursive)

Returns true if the specified directory contains the specified File.

Parameters:
directory - the directory to search in
f - the File to search
recursive - true to search recursive; false to search non recursive
Returns:
true if the specified directory contains the specified File; false otherwise

copyFile

public static void copyFile(File sourceFile,
                            File targetFile)
                     throws FileNotFoundException,
                            IOException

Copies the File denoted by sourcFile to the specified targetFile.

Parameters:
sourceFile - the source of the copy operation
targetFile - the traget of the copy operation
Throws:
FileNotFoundException - if either the sourcFile or the targetFile doesn't exist
IOException - if the copy operation failed due to an io error

isReadable

public static boolean isReadable(File f)

Tests if the File denoted by f is readable, i.e. read access is guaranteed.

Parameters:
f - the File to test
Returns:
true if the specified File is readable; false otherwise

isWriteable

public static boolean isWriteable(File f)

Tests if the File denoted by f is writeable, i.e. write access is guaranteed.

Parameters:
f - the File to test
Returns:
true if the specified File is writeable; false otherwise