DLESE Tools
v1.6.0

org.dlese.dpc.util
Class Files

java.lang.Object
  extended by org.dlese.dpc.util.Files

public final class Files
extends Object

Contains methods for performing common operations on files and directories such as reading, moving, deleting and copying. Also contains methods used to encode and decode IDs from filenames.

Version:
$Id: Files.java,v 1.22 2009/03/20 23:34:00 jweather Exp $
Author:
John Weatherley, Dave Deniman

Field Summary
static char dirSep
          The char used to separate files in the native file system.
static String dirSepStr
          The String used to separate files in the native file system.
 
Constructor Summary
Files()
           
 
Method Summary
static boolean copy(File infile, File outfile)
          Copys the contents of one file to another, as bytes.
static boolean copyDir(File sourceDir, File destinationDir)
          Recursively copies the contents of one directory to another.
static String decode(String msg)
          Decode a file name.
static void deleteDirectory(File directory)
          Deletes a directory or file and all files and directories within.
static String encode(String msg)
          Encode a file name for file system compatibility.
static String escapeWindowsPath(String path)
          Escapes a Windows directory path by removing the first colon so that it can be used as the end part of another directory path.
static String fid(File f)
          Appends a path into a string in such a way that lexicographic sorting gives the same results as a walk of the file hierarchy.
static String fid2path(String fid)
          Converts an fid back to a file path.
static String fuid(File f)
          Appends a path and date into a string in such a way that lexicographic sorting gives the same results as a walk of the file hierarchy.
protected static String getDateStamp()
          Return a string for the current time and date, sutiable for display in log files and output to standout:
static char getFileSeparatorCh()
          Gets the String used to separate files in the native file system.
static String getFileSeparatorStr()
          Gets the char used to separate files in the native file system.
static String getIDFromFilename(File file)
          Gets a record ID from its file name.
static String getIDFromFilename(String filename)
          Gets a record ID from its file name.
static boolean move(File infile, File outfile)
          Moves a file from one location to another.
protected  void prtln(String s)
          Output a line of text to standard out, with datestamp, if debug is set to true.
protected static void prtlnErr(String s)
          Output a line of text to error out, with datestamp.
static StringBuffer readFile(File f)
          Reads a file into a StringBuffer.
static StringBuffer readFile(String filePath)
          Reads a file into a StringBuffer.
static StringBuffer readFileFromJarClasspath(String filePathInJarClasspath)
          Reads a file from a JAR or location in the runtime classpath.
static StringBuffer readFileToEncoding(File f, String encoding)
          Reads a file to a StringBuffer that will be in the given character encoding, for example UTF-8.
static StringBuffer readInputStream(InputStream is)
          Reads an InputStream into a StringBuffer and closes the stream when done.
static void setDebug(boolean db)
          Sets the debug attribute of the object
static boolean touch(File file)
          Resets the file mod time to the current time.
static boolean touch(String filePath)
          Resets the file mod time to the current time.
static void writeFile(StringBuffer content, File file)
          Writes a file containing the given content using UTF-8 encoding.
static void writeFile(StringBuffer content, String filePath)
          Writes a file containing the given content using UTF-8 encoding.
static void writeFile(String content, File file)
          Writes a file containing the given content using UTF-8 encoding.
static void writeFile(String content, String filePath)
          Writes a file containing the given content using UTF-8 encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dirSep

public static char dirSep
The char used to separate files in the native file system.


dirSepStr

public static String dirSepStr
The String used to separate files in the native file system.

Constructor Detail

Files

public Files()
Method Detail

getFileSeparatorStr

public static String getFileSeparatorStr()
Gets the char used to separate files in the native file system.

Returns:
The fileSeparatorStr value

getFileSeparatorCh

public static char getFileSeparatorCh()
Gets the String used to separate files in the native file system.

Returns:
The fileSeparatorCh value

touch

public static boolean touch(String filePath)
Resets the file mod time to the current time.

Parameters:
filePath - A path to a file.
Returns:
true if and only if the operation succeeded; false otherwise.

touch

public static boolean touch(File file)
Resets the file mod time to the current time.

Parameters:
file - A File.
Returns:
true if and only if the operation succeeded; false otherwise.

readFileFromJarClasspath

public static final StringBuffer readFileFromJarClasspath(String filePathInJarClasspath)
Reads a file from a JAR or location in the runtime classpath.

Parameters:
filePathInJarClasspath - The path to the file inside the JAR file or classpath
Returns:
The content of the file

readFile

public static final StringBuffer readFile(String filePath)
                                   throws IOException
Reads a file into a StringBuffer.

Parameters:
filePath - The path to the file.
Returns:
A StringBuffer containing the files content.
Throws:
IOException - If error.

readFile

public static final StringBuffer readFile(File f)
                                   throws IOException
Reads a file into a StringBuffer.

Parameters:
f - The file.
Returns:
A StringBuffer containing the files content.
Throws:
IOException - If error.

readFileToEncoding

public static final StringBuffer readFileToEncoding(File f,
                                                    String encoding)
                                             throws IOException
Reads a file to a StringBuffer that will be in the given character encoding, for example UTF-8. The file that is read is assumed to be in the default system character encoding. See Converting Non-Unicode Text Tutorial .

Parameters:
f - The File to read
encoding - The encoding to write to, or null to use the default system encoding
Returns:
The content of the File in the given encoding
Throws:
IOException - If error

readInputStream

public static final StringBuffer readInputStream(InputStream is)
                                          throws IOException
Reads an InputStream into a StringBuffer and closes the stream when done.

Parameters:
is - The InputStream.
Returns:
A StringBuffer containing the stream content.
Throws:
IOException - If error.

writeFile

public static final void writeFile(String content,
                                   File file)
                            throws IOException
Writes a file containing the given content using UTF-8 encoding.

Parameters:
content - The content to write.
file - The file to write to.
Throws:
IOException - If error

writeFile

public static final void writeFile(String content,
                                   String filePath)
                            throws IOException
Writes a file containing the given content using UTF-8 encoding.

Parameters:
content - The content to write.
filePath - The file to write to.
Throws:
IOException - If error

writeFile

public static final void writeFile(StringBuffer content,
                                   File file)
                            throws IOException
Writes a file containing the given content using UTF-8 encoding.

Parameters:
content - The content to write.
file - The file to write to.
Throws:
IOException - If error.

writeFile

public static final void writeFile(StringBuffer content,
                                   String filePath)
                            throws IOException
Writes a file containing the given content using UTF-8 encoding.

Parameters:
content - The content to write.
filePath - The file to write to.
Throws:
IOException - If error.

copyDir

public static final boolean copyDir(File sourceDir,
                                    File destinationDir)
                             throws Exception
Recursively copies the contents of one directory to another. The source and destination File objects must be directories and must exist, otherwise nothing is done. Existing files in the destination directory with the same name as those in the source directory are overwritten.

Parameters:
sourceDir - The directory to copy.
destinationDir - The directory to copy to.
Returns:
True if successful.
Throws:
Exception - If error.

copy

public static final boolean copy(File infile,
                                 File outfile)
                          throws Exception
Copys the contents of one file to another, as bytes. E.g., does not convert from and then back to characters. May be used for both text and binary files.

Parameters:
infile - The file to copy.
outfile - The destination of the copied file.
Returns:
True if successful.
Throws:
Exception - If error.

move

public static boolean move(File infile,
                           File outfile)
Moves a file from one location to another.

Parameters:
infile - The file to move.
outfile - A file denoting the new location.
Returns:
True if successful.

deleteDirectory

public static void deleteDirectory(File directory)
                            throws SecurityException
Deletes a directory or file and all files and directories within.

Parameters:
directory - The directory to delete.
Throws:
Exception - If error.
SecurityException

escapeWindowsPath

public static String escapeWindowsPath(String path)
Escapes a Windows directory path by removing the first colon so that it can be used as the end part of another directory path. For example, if the path is C:/mydirectory/myfile.txt the output of this method will be C/mydirectory/myfile.txt. If the path does not begin with an upper-case character and a colon then nothing is done.

Parameters:
path - The original path.
Returns:
The escaped path with the first colon removed.

getIDFromFilename

public static String getIDFromFilename(File file)
Gets a record ID from its file name. Assumes the file name is of the form ID.xml where ID is the record id. For example DLESE-000-000-000-001.xml.

Parameters:
file - The file.
Returns:
The record ID of the file.

getIDFromFilename

public static String getIDFromFilename(String filename)
Gets a record ID from its file name. Assumes the file name is of the form ID.xml where ID is the record id. For example DLESE-000-000-000-001.xml.

Parameters:
filename - The file.
Returns:
The record ID of the file.

fid

public static String fid(File f)
Appends a path into a string in such a way that lexicographic sorting gives the same results as a walk of the file hierarchy. Thus null () is used both to separate directory components. May be decoded back to a file path using fid2path(String fid).

Parameters:
f - The file whoes path will be encoded.
Returns:
The encoded fid.

fuid

public static String fuid(File f)
Appends a path and date into a string in such a way that lexicographic sorting gives the same results as a walk of the file hierarchy. Thus null () is used both to separate directory components and to separate the path from the date.

Parameters:
f - The file whoes path will be encoded.
Returns:
The encoded fuid.

fid2path

public static String fid2path(String fid)
Converts an fid back to a file path. The reciprocol of fid(File f).

Parameters:
fid - The fid.
Returns:
The file path that corresponds to this fid.

encode

public static String encode(String msg)
                     throws Exception
Encode a file name for file system compatibility.

Parameters:
msg - A String to encode.
Returns:
Encoded String
Throws:
Exception - If error.

decode

public static String decode(String msg)
                     throws Exception
Decode a file name.

Parameters:
msg - Message to decode
Returns:
Decoded String
Throws:
Exception - If unable to decode.

getDateStamp

protected static final String getDateStamp()
Return a string for the current time and date, sutiable for display in log files and output to standout:

Returns:
The dateStamp value

prtlnErr

protected static final void prtlnErr(String s)
Output a line of text to error out, with datestamp.

Parameters:
s - The text that will be output to error out.

prtln

protected final void prtln(String s)
Output a line of text to standard out, with datestamp, if debug is set to true.

Parameters:
s - The String that will be output.

setDebug

public static void setDebug(boolean db)
Sets the debug attribute of the object

Parameters:
db - The new debug value

DLESE Tools
v1.6.0