org.metasyntactic.io
Class FileUtilities

java.lang.Object
  |
  +--org.metasyntactic.io.FileUtilities

public class FileUtilities
extends java.lang.Object

Contains useful static methods for manipulating files and directory structures.


Field Summary
static int IGNORE_CASE
           
static int REVERSE_SORT
           
static int SORT_BY_DATE
          Flag to tell the comparator to sort files by their last modifed date
static int SORT_BY_EXTENSION
          Flag to tell the comparator to sort files by their extensions.
static int SORT_BY_NAME
          Flag to tell the comparator to sort files by their name
static int SORT_BY_SIZE
          Flag to tell the comparator to sort files by their size
 
Constructor Summary
FileUtilities()
           
 
Method Summary
static void copy(java.io.File from, java.io.File to)
           
static java.util.Comparator fileComparator()
          Returns a comparator that will compare files only by name, (not including its path).
static java.util.Comparator fileComparator(int criteria)
          Returns a Comparator that can be used (for example) as the comparator passed to java.util.Arrays.sort(Object[], Comparator).
static void growFile(java.io.File file, byte[] content, long position)
          Grows file by inserting the byte array into the file at the location specified
static void growFile(java.io.File file, int length, byte val, long position)
          Grows file by inserting 'length' bytes of value 'val' into the file at the position specified
static void growFile(java.io.File file, int length, long position)
          Grows file by inserting 'length' zero'ed bytes at the position specified
static void main(java.lang.String[] args)
           
static void recurse(java.io.File directory, boolean applyOperatorToDirectories, Operator operator)
          Recurses all the files and directories reachable from this directory and operates on them in the manner implemented in the Operator.operate method.
static void simpleRecurse(java.io.File directory, boolean applyOperatorToDirectories, Operator operator)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SORT_BY_EXTENSION

public static final int SORT_BY_EXTENSION
Flag to tell the comparator to sort files by their extensions. The extension is defined to be the characters found after the last . (period) in the file name. Note: if there is no . in the file name, then the entire name is taken to be the extension

See Also:
Constant Field Values

SORT_BY_NAME

public static final int SORT_BY_NAME
Flag to tell the comparator to sort files by their name

See Also:
Constant Field Values

SORT_BY_SIZE

public static final int SORT_BY_SIZE
Flag to tell the comparator to sort files by their size

See Also:
Constant Field Values

SORT_BY_DATE

public static final int SORT_BY_DATE
Flag to tell the comparator to sort files by their last modifed date

See Also:
Constant Field Values

IGNORE_CASE

public static final int IGNORE_CASE
See Also:
Constant Field Values

REVERSE_SORT

public static final int REVERSE_SORT
See Also:
Constant Field Values
Constructor Detail

FileUtilities

public FileUtilities()
Method Detail

fileComparator

public static java.util.Comparator fileComparator()
Returns a comparator that will compare files only by name, (not including its path).

Returns:
A comparator that will sort files in order by name.

fileComparator

public static java.util.Comparator fileComparator(int criteria)
Returns a Comparator that can be used (for example) as the comparator passed to java.util.Arrays.sort(Object[], Comparator). The comparator is based on the criteria you specify.

Criteria includes:

Note: even if you choose REVERSE_SORT, directories will appear before files. However, the list of directories, and the list of files will both appear in the reverse order of normal. To indicate that you want the files sorted in reverse order, 'bitwise or' ( | ) REVERSE_SORT with the sorting option you want. If an invalied sorting code is passed to this method, it will default to sorting by name.

Examples:

Parameters:
criteria - The type of sort. | this value with REVERSE_SORT to get the reverse effect on the list of directories and files.
Returns:
A comparator that can compare a list of files in the manner use specified with 'criteria'

recurse

public static void recurse(java.io.File directory,
                           boolean applyOperatorToDirectories,
                           Operator operator)
Recurses all the files and directories reachable from this directory and operates on them in the manner implemented in the Operator.operate method. The boolean specifies whether or not to apply the operator to directories as well as files.

Two important things to note. First: this method is thread safe. You can call it from one thread on one directory, and another thread on another directory and will get the proper results. Second: this method is intelligent when it comes to directory structure. It will not recurse directories it has already seen.

Parameters:
directory - The directory to start recursing from
operator - The operator to apply to the files recahable from this directory (and possibly the directories as well)
applyOperatorToDirectories - Should the operator be applied to the Directories? Note! If this is true the directory passed in will be operated on as well

simpleRecurse

public static void simpleRecurse(java.io.File directory,
                                 boolean applyOperatorToDirectories,
                                 Operator operator)

growFile

public static void growFile(java.io.File file,
                            byte[] content,
                            long position)
                     throws java.io.IOException
Grows file by inserting the byte array into the file at the location specified

java.io.IOException

growFile

public static void growFile(java.io.File file,
                            int length,
                            long position)
                     throws java.io.IOException
Grows file by inserting 'length' zero'ed bytes at the position specified

java.io.IOException

growFile

public static void growFile(java.io.File file,
                            int length,
                            byte val,
                            long position)
                     throws java.io.IOException
Grows file by inserting 'length' bytes of value 'val' into the file at the position specified

java.io.IOException

copy

public static void copy(java.io.File from,
                        java.io.File to)
                 throws java.io.IOException
java.io.IOException

main

public static void main(java.lang.String[] args)