org.stringer.util
Class FileKit

java.lang.Object
  |
  +--org.stringer.util.FileKit

public class FileKit
extends java.lang.Object

Provides a set of static methods that help with file manipulation.

Author:
R. Kevin Cole kcole@users.sourceforge.net

Field Summary
private static org.apache.log4j.Logger logger
           
 
Constructor Summary
private FileKit()
          This class has a private constructor to prevent it from being instantiated.
 
Method Summary
static java.lang.String appendSeparator(java.lang.String path)
          Append a file separator to path if it does not have a trailing one.
static void copy(java.io.File source, java.io.File dest)
          Copies source to destination.
static java.io.File createUnique(java.lang.String filename)
          Creates a unique file by inserting digits into filename.
static java.io.File createUnique(java.lang.String path, java.lang.String filename)
          Creates a unique file in by inserting digits into filename.
static java.lang.String directories(java.lang.String dirs)
           
static java.lang.String directory(java.lang.String dir)
           
static boolean exists(java.lang.String filename)
          Returns true, if filename exists; false, otherwise.
static java.lang.String getExtension(java.io.File file)
           
static java.lang.String getExtension(java.lang.String filename)
          Extract the extension from a filename.
static java.lang.String getHomeDir()
          Returns the absolute path of the ~/.stringer/ directory.
static java.lang.String getHomeDir(java.lang.String subdir)
          Checks for existence of .stringer folder in the user's home directory and returns the absolute path with a file separator appended.
static java.lang.String getUniqueName(java.lang.String filename)
          Creates unique filename.
static java.lang.String getUniqueName(java.lang.String filename, java.lang.String infix)
           
static boolean move(java.io.File source, java.io.File dest)
          Move a file from source to destination.
static java.io.File moveUnique(java.io.File file, java.lang.String path)
          Move a file to path.
static java.io.File moveUnique(java.io.File file, java.lang.String path, java.lang.String filename)
          Moves file to path but renames filename if it already exists in the target path.
static java.lang.String name(java.lang.String filename)
           
static void readBinary(java.io.File file, java.util.Collection c)
          Reads all objects from file using serialization and adds them to c.
static java.io.File setExtension(java.io.File file, java.lang.String ext)
          Appends the specified extension to a file if it is not already there.
static void shorten(java.io.File file, long bytes)
          Shortens file by bytes bytes.
static void writeBinary(java.io.File file, java.util.Collection c)
          Write all items in c to file using serialization.
static void writeProperties(java.io.File file, java.util.Properties props)
          Stores props in file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static org.apache.log4j.Logger logger
Constructor Detail

FileKit

private FileKit()
This class has a private constructor to prevent it from being instantiated.

Method Detail

createUnique

public static java.io.File createUnique(java.lang.String filename)
                                 throws java.io.IOException
Creates a unique file by inserting digits into filename. This using the filename argument as a pattern to create a unique filename. If the pattern looks like "subscription.xml" the unique filename of the File returned will look like "subscription.1.xml".

Returns:
the created file
java.io.IOException

createUnique

public static java.io.File createUnique(java.lang.String path,
                                        java.lang.String filename)
                                 throws java.io.IOException
Creates a unique file in by inserting digits into filename.

Parameters:
path - the path to a directory in the file system to create
filename - the template filename of the file to be created
Returns:
the created file
java.io.IOException

moveUnique

public static java.io.File moveUnique(java.io.File file,
                                      java.lang.String path,
                                      java.lang.String filename)
                               throws java.io.IOException
Moves file to path but renames filename if it already exists in the target path.

Parameters:
file - the file to be moved
path - the file is moved to this location in the file system.
filename - the new filename for file
Returns:
the file that was moved.
java.io.IOException

move

public static boolean move(java.io.File source,
                           java.io.File dest)
                    throws java.io.IOException
Move a file from source to destination. An attempt is made to simply rename the file to the destination and if that fails, the file is copied to the destination and the source is then removed.

java.io.IOException

moveUnique

public static java.io.File moveUnique(java.io.File file,
                                      java.lang.String path)
                               throws java.io.IOException
Move a file to path.

java.io.IOException

copy

public static void copy(java.io.File source,
                        java.io.File dest)
                 throws java.io.IOException
Copies source to destination. If dest already exists it is overwritten.

Parameters:
source - the source file
dest - the destination file
java.io.IOException

name

public static java.lang.String name(java.lang.String filename)
Parameters:
filename - the filename to be processed.
Returns:
the name of a file without the extension.

getExtension

public static java.lang.String getExtension(java.lang.String filename)
Extract the extension from a filename.

Parameters:
filename - the filename to be processed.
Returns:
a filename's extension converted to lower case

getExtension

public static java.lang.String getExtension(java.io.File file)

setExtension

public static java.io.File setExtension(java.io.File file,
                                        java.lang.String ext)
Appends the specified extension to a file if it is not already there.

Parameters:
file - The extension of this file is set to ext
ext - the extension to add to file
Returns:
a new file with the extension set as requested.

exists

public static boolean exists(java.lang.String filename)
Returns true, if filename exists; false, otherwise.


getHomeDir

public static final java.lang.String getHomeDir(java.lang.String subdir)
Checks for existence of .stringer folder in the user's home directory and returns the absolute path with a file separator appended.

Parameters:
subdir - a sub directory that is located in ~/.stringer/ or created if it does not exist
Returns:
empty string, if subdir could not be created; absolute path, otherwise

getHomeDir

public static final java.lang.String getHomeDir()
Returns the absolute path of the ~/.stringer/ directory.


appendSeparator

public static java.lang.String appendSeparator(java.lang.String path)
Append a file separator to path if it does not have a trailing one.


directory

public static java.lang.String directory(java.lang.String dir)

directories

public static java.lang.String directories(java.lang.String dirs)

shorten

public static void shorten(java.io.File file,
                           long bytes)
Shortens file by bytes bytes.


writeProperties

public static void writeProperties(java.io.File file,
                                   java.util.Properties props)
                            throws java.io.IOException
Stores props in file.

java.io.IOException

readBinary

public static void readBinary(java.io.File file,
                              java.util.Collection c)
                       throws java.io.IOException
Reads all objects from file using serialization and adds them to c.

java.io.IOException

writeBinary

public static void writeBinary(java.io.File file,
                               java.util.Collection c)
                        throws java.io.IOException
Write all items in c to file using serialization.

java.io.IOException

getUniqueName

public static java.lang.String getUniqueName(java.lang.String filename)
Creates unique filename.


getUniqueName

public static java.lang.String getUniqueName(java.lang.String filename,
                                             java.lang.String infix)


Copyright © R. Kevin Cole. All Rights Reserved.