Package org.dlese.dpc.util
Class Files
java.lang.Object
org.dlese.dpc.util.Files
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
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final booleanCopys the contents of one file to another, as bytes.static final booleanRecursively copies the contents of one directory to another.static StringDecode a file name.static voiddeleteDirectory(File directory) Deletes a directory or file and all files and directories within.static StringEncode a file name for file system compatibility.static StringescapeWindowsPath(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 StringAppends a path into a string in such a way that lexicographic sorting gives the same results as a walk of the file hierarchy.static StringConverts an fid back to a file path.static StringAppends 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 final StringReturn a string for the current time and date, sutiable for display in log files and output to standout:static charGets the String used to separate files in the native file system.static StringGets the char used to separate files in the native file system.static StringgetIDFromFilename(File file) Gets a record ID from its file name.static StringgetIDFromFilename(String filename) Gets a record ID from its file name.static booleanMoves a file from one location to another.protected final voidOutput a line of text to standard out, with datestamp, if debug is set to true.protected static final voidOutput a line of text to error out, with datestamp.static final StringBufferReads a file into a StringBuffer.static final StringBufferReads a file into a StringBuffer.static final StringBufferreadFileFromJarClasspath(String filePathInJarClasspath) Reads a file from a JAR or location in the runtime classpath.static final StringBufferreadFileToEncoding(File f, String encoding) Reads a file to a StringBuffer that will be in the given character encoding, for example UTF-8.static final StringBufferReads an InputStream into a StringBuffer and closes the stream when done.static voidsetDebug(boolean db) Sets the debug attribute of the objectstatic booleanResets the file mod time to the current time.static booleanResets the file mod time to the current time.static final voidwriteFile(StringBuffer content, File file) Writes a file containing the given content using UTF-8 encoding.static final voidwriteFile(StringBuffer content, String filePath) Writes a file containing the given content using UTF-8 encoding.static final voidWrites a file containing the given content using UTF-8 encoding.static final voidWrites a file containing the given content using UTF-8 encoding.
-
Field Details
-
dirSep
public static char dirSepThe char used to separate files in the native file system. -
dirSepStr
The String used to separate files in the native file system.
-
-
Constructor Details
-
Files
public Files()
-
-
Method Details
-
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
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
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
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
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
Reads a file into a StringBuffer.- Parameters:
f- The file.- Returns:
- A StringBuffer containing the files content.
- Throws:
IOException- If error.
-
readFileToEncoding
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 readencoding- 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
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
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
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
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
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
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
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
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
Deletes a directory or file and all files and directories within.- Parameters:
directory- The directory to delete.- Throws:
Exception- If error.SecurityException
-
escapeWindowsPath
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
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
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
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 usingfid2path(String fid).- Parameters:
f- The file whoes path will be encoded.- Returns:
- The encoded fid.
-
fuid
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
Converts an fid back to a file path. The reciprocol offid(File f).- Parameters:
fid- The fid.- Returns:
- The file path that corresponds to this fid.
-
encode
Encode a file name for file system compatibility.- Parameters:
msg- A String to encode.- Returns:
- Encoded String
- Throws:
Exception- If error.
-
decode
Decode a file name.- Parameters:
msg- Message to decode- Returns:
- Decoded String
- Throws:
Exception- If unable to decode.
-
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
Output a line of text to error out, with datestamp.- Parameters:
s- The text that will be output to error out.
-
prtln
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
-