org.jasen.util
Class FileUtils

java.lang.Object
  extended byorg.jasen.util.FileUtils

public class FileUtils
extends Object

General file utility methods.

Author:
Jason Polites

Nested Class Summary
static class FileUtils.ExtensionFileFilter
           Inner file filter class for listing files of known extension.
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void copy(File source, File destination)
          Convenience file copy method
static void dedupe(File folder, boolean filename)
          Removes (deletes) all duplicate files found in the given folder.
static void dedupe(File folder, File deposit, boolean filename)
          Removes all duplicate files found in the given folder and moves them to the deposit folder
static void dedupe(File folder, FileFilter filter, boolean filename)
          Removes (deletes) all duplicate files found in the given folder with the given filter
static void dedupe(File folder, FileFilter filter, File deposit, boolean filename)
          Removes duplicate files from the given folder by renaming them to the given extension.
static String fingerPrintFile(File file, int length)
          Creates a distinct "fingerprint" of the given file such that two files with the same content will have the same fingerprint.
static String getAbsolutePathWithoutFile(File pathname)
          Gets the absolute path without the file (root path).
static String getFileExtension(File pathname)
          Returns the String that occurs after the last "dot" in the filename
static String getFileExtension(String filename)
           
static String getFilenameWithoutExtension(String filename)
          Gets the name of a file without the extension (text after last dot)
static String getSafePath(String path)
          Ensures the path is terminated with a file separator
static URL[] listFiles(File folder, String extension)
          Lists all the files in the given folder with the given extension
static URL[] listJars(File folder)
          Lists all the .jar files in the given folder as URL references
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

public FileUtils()
Method Detail

dedupe

public static void dedupe(File folder,
                          boolean filename)
                   throws IOException
Removes (deletes) all duplicate files found in the given folder.

Parameters:
folder -
filename - If true, files with the same root name are considered duplicates irrespective of their fingerprint
Throws:
IOException

dedupe

public static void dedupe(File folder,
                          File deposit,
                          boolean filename)
                   throws IOException
Removes all duplicate files found in the given folder and moves them to the deposit folder

Parameters:
folder -
deposit -
filename - If true, files with the same root name are considered duplicates irrespective of their fingerprint
Throws:
IOException

dedupe

public static void dedupe(File folder,
                          FileFilter filter,
                          boolean filename)
                   throws IOException
Removes (deletes) all duplicate files found in the given folder with the given filter

Parameters:
folder -
filter -
filename - If true, files with the same root name are considered duplicates irrespective of their fingerprint
Throws:
IOException

dedupe

public static void dedupe(File folder,
                          FileFilter filter,
                          File deposit,
                          boolean filename)
                   throws IOException
Removes duplicate files from the given folder by renaming them to the given extension.

If extension is null, the duplicate files are deleted.

If more than one duplicate of the same file is found, an integer count is appended to the renamed file.

Parameters:
folder - The folder in which to look for duplicates
filter - The file filter to use when listing files
deposit - The path to which duplicates are moved (must be a directory)
filename - If true, files with the same root name are considered duplicates irrespective of their fingerprint
Throws:
IOException

fingerPrintFile

public static String fingerPrintFile(File file,
                                     int length)
                              throws IOException
Creates a distinct "fingerprint" of the given file such that two files with the same content will have the same fingerprint.

Parameters:
file - The file to fingerprint
length - The length of the fingerprint. The longer the length, the more accurate the fingerprint. NOTE: The size of the actual string returned will be greater than "length" bytes
Returns:
A String representing a non-unique representation of the file
Throws:
IOException

getAbsolutePathWithoutFile

public static String getAbsolutePathWithoutFile(File pathname)
Gets the absolute path without the file (root path).

Parameters:
pathname -
Returns:
The absolute path to the file excluding the filename

getSafePath

public static String getSafePath(String path)
Ensures the path is terminated with a file separator

Parameters:
path -
Returns:

getFilenameWithoutExtension

public static String getFilenameWithoutExtension(String filename)
Gets the name of a file without the extension (text after last dot)

Parameters:
filename -
Returns:
The root filename without its extension

getFileExtension

public static String getFileExtension(File pathname)
Returns the String that occurs after the last "dot" in the filename

Parameters:
pathname -
Returns:
The extension of the file

getFileExtension

public static String getFileExtension(String filename)

copy

public static final void copy(File source,
                              File destination)
                       throws IOException
Convenience file copy method

Parameters:
source -
destination -
Throws:
IOException

listJars

public static URL[] listJars(File folder)
                      throws MalformedURLException
Lists all the .jar files in the given folder as URL references

Parameters:
folder - The folder in which to look
Returns:
An array of java.net.URL objects
Throws:
MalformedURLException

listFiles

public static URL[] listFiles(File folder,
                              String extension)
                       throws MalformedURLException
Lists all the files in the given folder with the given extension

Parameters:
folder - The folder in which to look
extension - The file extension (case sensitive)
Returns:
An array of java.net.URL objects
Throws:
MalformedURLException