DLESE Tools
v1.2

org.dlese.dpc.util
Class Files

java.lang.Object
  extended byorg.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.

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.
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(BufferedReader rdr)
          Reads a BufferedReader into a StringBuffer.
static StringBuffer readFile(File f)
          Reads a file into a StringBuffer.
static StringBuffer readFile(String filePath)
          Reads a file into a StringBuffer.
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.

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.

readFile

public static final StringBuffer readFile(BufferedReader rdr)
                                   throws IOException
Reads a BufferedReader into a StringBuffer.

Parameters:
rdr - A BufferedReader.
Returns:
A StringBuffer containing the BufferedReaders content.
Throws:
IOException - If error.

writeFile

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

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

writeFile

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

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

writeFile

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

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

writeFile

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

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

copy

public static boolean copy(File infile,
                           File outfile)
                    throws Exception
Copys the contents of one file to another.

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 Exception
Deletes a directory or file and all files and directories within.

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

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.2