|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.opengts.util.FileTools
public class FileTools
File handling tools
Field Summary | |
---|---|
static int |
DEFAULT_URL_COPYFILE_TIMEOUT_MS
|
Constructor Summary | |
---|---|
FileTools()
|
Method Summary | |
---|---|
static boolean |
canExecute(java.io.File f)
Returns true if the file can be executed |
static boolean |
canRead(java.io.File f)
Returns true if the file can be read |
static boolean |
canWrite(java.io.File f)
Returns true if the file can be written |
static void |
closeStream(java.io.InputStream in)
Closes the specified InputStream. |
static void |
closeStream(java.io.OutputStream out)
Closes the specified OutputStream |
static int |
copyFile(java.net.URL inpURL,
java.io.File outFile)
Copies an input URL to an output file |
static int |
copyFile(java.net.URL inpURL,
java.io.File outFile,
boolean progress)
Copies an input URL to an output file |
static int |
copyFile(java.net.URL inpURL,
java.io.File outFile,
boolean progress,
int timeoutMS)
Copies an input URL to an output file |
static int |
copyFile(java.net.URL inpURL,
java.io.OutputStream out)
Copies an input URL to an output file |
static int |
copyFile(java.net.URL inpURL,
java.io.OutputStream out,
boolean progress)
Copies an input URL to an output file |
static int |
copyFile(java.net.URL inpURL,
java.io.OutputStream out,
boolean progress,
int timeoutMS)
Copies an input URL to an output file |
static int |
copyStreams(java.io.InputStream input,
java.io.OutputStream output)
Copies bytes from one stream to another |
static int |
copyStreams(java.io.InputStream input,
java.io.OutputStream output,
byte[] pattern,
int maxLen)
Copies bytes from one stream to another |
static int |
copyStreams(java.io.InputStream input,
java.io.OutputStream output,
byte[] pattern,
int maxLen,
boolean progress)
Copies bytes from one stream to another. If 'maxLen' is >= '0', then at most 'maxLen' bytes will be written to the output stream. |
static int |
copyStreams(java.io.InputStream input,
java.io.OutputStream output,
int maxLen)
Copies bytes from one stream to another |
static java.io.File |
findFile(java.io.File parentDir,
java.lang.String[] fileNames)
Returns the file path for the first file found in the specified list |
static java.util.List<java.lang.String> |
findPatternInFile(java.io.File file,
java.lang.String pattern,
boolean ignoreCase)
Search (grep) for the specified pattern in the specified file |
static java.lang.Object |
getAttribute(java.io.File file,
java.lang.String attr)
Gets the specified attribute for this file. |
static java.io.File |
getCygwinSymlinkFile(java.io.File link,
boolean makeAbsolute)
Gets the String path target of the Cygwin symbolic link (EXPERIMENTAL: May not work with all Cygwin symbolic link files) |
static java.io.File[] |
getDirectories(java.io.File dir)
Returns an array of sub-directories within the specified parent directory. |
static java.lang.String |
getExtension(java.io.File file)
Gets the shortest possible extension characters from the specified file. |
static java.lang.String |
getExtension(java.lang.String filePath)
Gets the extension characters from the specified file name |
static java.lang.Object |
getFilePath(java.io.File file)
Returns a java.nio.file.Path instance for this file |
static java.io.File[] |
getFiles(java.io.File dir)
Returns an array of files within the specified parent directory. |
static java.io.File[] |
getFiles(java.io.File dir,
java.util.regex.Pattern regex,
boolean recursiveDecent)
Returns an array of files within the specified parent directory. |
static java.io.File[] |
getFiles(java.io.File dir,
java.util.Set<java.lang.String> extnSet,
boolean recursiveDecent)
Returns an array of files within the specified parent directory which match the specified extensions. |
static java.io.File[] |
getFiles(java.io.File dir,
java.lang.String[] extnList,
boolean recursiveDecent)
Returns an array of files within the specified parent directory which match the specified extensions. |
static java.io.File[] |
getFiles(java.io.File dir,
java.lang.String fileGlob,
boolean recursiveDecent)
Returns an array of files within the specified parent directory. |
static java.io.File[] |
getFilesystemRoots()
Return an array of all filesystem 'root' directories |
static java.lang.String |
getGroupOwner(java.io.File file)
Gets the group owner of this file |
static java.lang.String |
getHash_MD5(byte[] data)
Returns the MD5 hash for the specified bytes |
static java.lang.String |
getHash_MD5(java.io.File file)
Returns the MD5 hash for the specified file |
static java.lang.String |
getHash_SHA1(byte[] data)
Returns the SHA-1 hash for the specified bytes |
static java.lang.String |
getHash_SHA1(java.io.File file)
Returns the SHA-1 hash for the specified bytes |
static java.lang.String |
getHash_SHA256(byte[] data)
Returns the SHA-256 hash for the specified bytes |
static java.lang.String |
getHash_SHA256(java.io.File file)
Returns the SHA-256 hash for the specified bytes |
static java.io.File |
getRealFile(java.io.File file)
Gets the real (canonical) file for the specified file/path |
static java.io.File |
getRealFile(java.io.File dir,
java.lang.String file)
Gets the real (canonical) file for the specified file/path |
static java.lang.String |
getUserOwner(java.io.File file)
Gets the user owner of this file |
static java.io.File |
getWindowsShortcutFile(java.io.File link,
boolean makeAbsolute)
Gets the String path target of the Windows shortcut. (EXPERIMENTAL: May not work with all Windows shortcut files) |
static boolean |
hasExtension(java.io.File file,
java.lang.String[] extn)
Returns true if the specified file has an extension which matches one of the extensions listed in the specified String array |
static boolean |
hasExtension(java.lang.String filePath,
java.lang.String[] extn)
Returns true if the specified file path has an extension which matches one of the extensions listed in the specified String array |
static boolean |
hasFileSeparator(java.lang.String fn)
Returns true if the specified String contains a file separator |
static boolean |
isAbsolute(java.io.File f)
Returns true if the specified path reference is absolute |
static boolean |
isCygwinSymlink(java.io.File link)
Returns true if the specified file is a Cygwin symbolic link (EXPERIMENTAL: May not work with all Cygwin symbolic link files) |
static boolean |
isDirectory(java.io.File d)
Returns true if the specified item is a directory |
static boolean |
isDirectory(java.lang.String d)
Returns true if the specified item is a directory |
static boolean |
isFile(java.io.File f)
Returns true if the specified item is a file |
static boolean |
isFile(java.io.File f,
java.lang.String ext)
Returns true if the specified item is a file with the specified extension |
static boolean |
isFile(java.lang.String f)
Returns true if the specified item is a file |
static boolean |
isFileSeparatorChar(char ch)
Returns true if the specified character is a file separator |
static boolean |
isSymbolicLink(java.io.File file)
Returns true if this file is a symbolic link |
static boolean |
isWindowsShortcut(java.io.File link)
Returns true if the specified file is a Windows shortcut file (EXPERIMENTAL: May not work with all Windows shortcut files) |
static void |
main(java.lang.String[] argv)
Debug/Testing entry point |
static java.io.InputStream |
openInputFile(java.io.File file)
Opens the specified file for reading |
static java.io.InputStream |
openInputFile(java.lang.String file)
Opens the specified file for reading |
static boolean |
readBoolean_stdin(java.lang.String msg,
boolean dft)
Prints a message, and reads a boolean value from stdin |
static double |
readDouble_stdin(java.lang.String msg,
double dft)
Prints a message, and reads a double value from stdin |
static byte[] |
readFile(java.io.File file)
Returns an array of bytes read from the specified file |
static byte[] |
readFile(java.io.File file,
int maxLen)
Returns an array of bytes read from the specified file |
static byte[] |
readFile(java.lang.String file)
Returns an array of bytes read from the specified file |
static java.lang.String |
readLine_stdin()
Reads a single line of characters from stdin, terminated by either a newline (\n) or carriage-return (\r) |
static java.lang.String |
readLine(java.io.InputStream input)
Reads a single line of characters from the specified InputStream, terminated by either a newline (\n) or carriage-return (\r) |
static java.lang.String |
readLine(java.io.InputStream input,
char eol)
Reads a single line of characters from the specified InputStream, terminated by either a newline (\n) or carriage-return (\r) |
static java.lang.String |
readLineNL(java.io.InputStream input)
Reads a single line of characters from the specified InputStream, terminated by a newline only (\n). |
static long |
readLong_stdin(java.lang.String msg,
long dft)
Prints a message, and reads a long value from stdin |
static byte[] |
readStream(java.io.InputStream input)
Returns an array of bytes read from the specified InputStream |
static byte[] |
readStream(java.io.InputStream input,
int maxLen)
Returns an array of bytes read from the specified InputStream |
static java.lang.String |
readString_stdin(java.lang.String msg,
java.lang.String dft)
Prints a message, and reads a line of text from stdin |
static java.lang.String |
removeExtension(java.io.File file)
Removes the extension from the specified file |
static java.lang.String |
removeExtension(java.lang.String filePath)
Removes the extension from the specified file path |
static java.io.File |
resolveCommand(java.lang.String cmd)
Resolves specified command relative to the environment "PATH" variable |
static boolean |
supportsFilesGetAttribute()
Returns true if "Files.getAttribute(...)" is supported. |
static java.io.File |
toFile(java.io.File base,
java.lang.String[] path)
Convert a URL specification to a File |
static java.io.File |
toFile(java.lang.String[] path)
Convert a URL specification to a File |
static java.io.File |
toFile(java.net.URL url)
Convert a URL specification to a File |
static java.net.URL |
toURL(java.io.File file)
Convert a File specification to a URL |
static boolean |
traverseAllFiles(java.io.File dir,
java.io.FileFilter filter)
Executed the filter "accept" callback on all files within the specified directory |
static boolean |
writeEscapedUnicode(java.lang.String dataStr,
java.io.File file)
Writes a String to the specified file in "ISO-8859-1" character encoding. Unicode characters are escaped using the ' ' format. |
static boolean |
writeFile(byte[] data,
java.io.File file)
Writes a byte array to the specified file |
static boolean |
writeFile(byte[] data,
java.io.File file,
boolean append)
Writes a byte array to the specified file |
static void |
writeStream(java.io.OutputStream output,
java.lang.String dataStr)
Writes a String to the specified OutputStream |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int DEFAULT_URL_COPYFILE_TIMEOUT_MS
Constructor Detail |
---|
public FileTools()
Method Detail |
---|
public static java.net.URL toURL(java.io.File file) throws java.net.MalformedURLException
file
- The file specification
java.net.MalformedURLException
- If a protocol handler for the URL could not be found,
or if some other error occurred while constructing the URLpublic static java.io.File toFile(java.net.URL url) throws java.net.URISyntaxException
url
- The URL specification
URIsyntaxException
- if the protocol was not 'file' or URL could
otherwise not be converted to a file
java.net.URISyntaxException
public static java.io.File toFile(java.io.File base, java.lang.String[] path)
base
- The base file specificationpath
- The file path directories (last element may be a file name)
public static java.io.File toFile(java.lang.String[] path)
path
- The file path directories (last element may be a file name)
public static java.io.File getRealFile(java.io.File file)
file
- The file for which the real file will be returned
public static java.io.File getRealFile(java.io.File dir, java.lang.String file)
dir
- The directory containing the specified file namefile
- The file for which the real file will be returned
public static int copyFile(java.net.URL inpURL, java.io.File outFile) throws java.io.IOException
inpURL
- The URL from which data will be readoutFile
- The File to whish data will be written
java.io.IOException
public static int copyFile(java.net.URL inpURL, java.io.File outFile, boolean progress) throws java.io.IOException
inpURL
- The URL from which data will be readoutFile
- The File to whish data will be writtenprogress
- Show progress
java.io.IOException
public static int copyFile(java.net.URL inpURL, java.io.File outFile, boolean progress, int timeoutMS) throws java.io.IOException
inpURL
- The URL from which data will be readoutFile
- The File to whish data will be writtenprogress
- Show progresstimeoutMS
- The timeout in milliseconds (0 for infinite timeout)
java.io.IOException
public static int copyFile(java.net.URL inpURL, java.io.OutputStream out) throws java.io.IOException
inpURL
- The URL from which data will be readout
- The OutputStream to whish data will be written
java.io.IOException
public static int copyFile(java.net.URL inpURL, java.io.OutputStream out, boolean progress) throws java.io.IOException
inpURL
- The URL from which data will be readout
- The OutputStream to whish data will be writtenprogress
- Show progress
java.io.IOException
public static int copyFile(java.net.URL inpURL, java.io.OutputStream out, boolean progress, int timeoutMS) throws java.io.IOException
inpURL
- The URL from which data will be readout
- The OutputStream to whish data will be writtenprogress
- Show progresstimeoutMS
- The timeout in milliseconds (0 for infinite timeout)
java.io.IOException
public static int copyStreams(java.io.InputStream input, java.io.OutputStream output) throws java.io.IOException
input
- The InputStreamoutput
- The OutputStream
java.io.IOException
- if an I/O error occurspublic static int copyStreams(java.io.InputStream input, java.io.OutputStream output, int maxLen) throws java.io.IOException
input
- The InputStreamoutput
- The OutputStreammaxLen
- The maximum number of bytes to copy
java.io.IOException
- if an I/O error occurspublic static int copyStreams(java.io.InputStream input, java.io.OutputStream output, byte[] pattern, int maxLen) throws java.io.IOException
input
- The InputStreamoutput
- The OutputStreampattern
- The pattern to match, to terminate the copymaxLen
- The maximum number of bytes to copy
java.io.IOException
- if an I/O error occurspublic static int copyStreams(java.io.InputStream input, java.io.OutputStream output, byte[] pattern, int maxLen, boolean progress) throws java.io.EOFException, java.io.IOException
input
- The InputStreamoutput
- The OutputStreampattern
- The pattern to match, to terminate the copymaxLen
- The maximum number of bytes to copyprogress
- Show progress (to stdout)
java.io.EOFException
- if the end of stream is reached before the pattern is found
java.io.IOException
- if an I/O error occurs (on either input or output)public static java.io.InputStream openInputFile(java.lang.String file)
file
- The path of the file to open
public static java.io.InputStream openInputFile(java.io.File file)
file
- The file to open
public static void closeStream(java.io.InputStream in)
in
- The InputStream to closepublic static void closeStream(java.io.OutputStream out)
out
- The OutputStream to closepublic static byte[] readStream(java.io.InputStream input) throws java.io.IOException
input
- The InputStream
java.io.IOException
- if an I/O error occurspublic static byte[] readStream(java.io.InputStream input, int maxLen) throws java.io.IOException
input
- The InputStreammaxLen
- The maximum number of bytes to read from the stream
java.io.IOException
- if an I/O error occurspublic static void writeStream(java.io.OutputStream output, java.lang.String dataStr) throws java.io.IOException
output
- The OutputStreamdataStr
- The String to write to the OutputStream
java.io.IOException
- if an I/O error occurspublic static byte[] readFile(java.lang.String file)
file
- The file path from which the byte array is read
public static byte[] readFile(java.io.File file)
file
- The file from which the byte array is read
public static byte[] readFile(java.io.File file, int maxLen)
file
- The file from which the byte array is readmaxLen
- The maximum number of bytes to read from the file
public static java.lang.String readLine(java.io.InputStream input, char eol) throws java.io.IOException
input
- The InputStreameol
- The specific end-of-line character to search for
java.io.EOFException
- if the end of the input stream is encountered
java.io.IOException
- if an I/O error occurspublic static java.lang.String readLine(java.io.InputStream input) throws java.io.IOException
input
- The InputStream
java.io.EOFException
- if the end of the input stream is encountered
java.io.IOException
- if an I/O error occurspublic static java.lang.String readLineNL(java.io.InputStream input) throws java.io.IOException
input
- The InputStream
java.io.EOFException
- if the end of the input stream is encountered
java.io.IOException
- if an I/O error occurspublic static java.lang.String readLine_stdin() throws java.io.IOException
java.io.IOException
- if an I/O error occurspublic static java.lang.String readString_stdin(java.lang.String msg, java.lang.String dft) throws java.io.IOException
msg
- The message to printdft
- The default String returned, if no text was entered
java.io.IOException
- if an I/O error occurspublic static boolean readBoolean_stdin(java.lang.String msg, boolean dft) throws java.io.IOException
msg
- The message to printdft
- The default boolean value returned, if no value was entered
java.io.IOException
- if an I/O error occurspublic static long readLong_stdin(java.lang.String msg, long dft) throws java.io.IOException
msg
- The message to printdft
- The default long value returned, if no value was entered
java.io.IOException
- if an I/O error occurspublic static double readDouble_stdin(java.lang.String msg, double dft) throws java.io.IOException
msg
- The message to printdft
- The default double value returned, if no value was entered
java.io.IOException
- if an I/O error occurspublic static boolean writeFile(byte[] data, java.io.File file) throws java.io.IOException
data
- The byte array to write to the filefile
- The file to which the byte array is written
java.io.IOException
- if an I/O error occurspublic static boolean writeFile(byte[] data, java.io.File file, boolean append) throws java.io.IOException
data
- The byte array to write to the filefile
- The file to which the byte array is writtenappend
- True to append the bytes to the file, false to overwrite.
java.io.IOException
- if an error occurred.public static boolean writeEscapedUnicode(java.lang.String dataStr, java.io.File file) throws java.io.IOException
dataStr
- The String to write to the filefile
- The file to which the byte array is written
java.io.IOException
- if an error occurred.public static java.util.List<java.lang.String> findPatternInFile(java.io.File file, java.lang.String pattern, boolean ignoreCase)
file
- The target filepattern
- The pattern StringignoreCase
- True to ignore case
public static java.lang.String getExtension(java.lang.String filePath)
filePath
- The file name
public static java.lang.String getExtension(java.io.File file)
file
- The file
public static boolean hasExtension(java.lang.String filePath, java.lang.String[] extn)
filePath
- The file path/nameextn
- An array of file extensions
public static boolean hasExtension(java.io.File file, java.lang.String[] extn)
file
- The fileextn
- An array of file extensions
public static java.lang.String removeExtension(java.lang.String filePath)
filePath
- The file path from which the extension will be removed
public static java.lang.String removeExtension(java.io.File file)
file
- The file from which the extension will be removed
public static boolean isFileSeparatorChar(char ch)
ch
- The character to test
public static boolean hasFileSeparator(java.lang.String fn)
fn
- The String file path
public static java.io.File resolveCommand(java.lang.String cmd)
cmd
- The command name. If the specified command is an absolute path,
then the specified command path will me returned as-is.
public static boolean isAbsolute(java.io.File f)
f
- The file to test for absolute
public static boolean isFile(java.lang.String f)
f
- The item to check
public static boolean isFile(java.io.File f)
f
- The item to check
public static boolean isFile(java.io.File f, java.lang.String ext)
f
- The item to checkext
- The required file extension
public static boolean isDirectory(java.lang.String d)
d
- The item to check
public static boolean isDirectory(java.io.File d)
d
- The item to check
public static boolean isWindowsShortcut(java.io.File link)
link
- A file possibly representing a Windows shortcut file
public static java.io.File getWindowsShortcutFile(java.io.File link, boolean makeAbsolute)
link
- The Windows shortcut file.makeAbsolute
- If true and the shortcut is not absolute, the returned file will
be made relative to the parent directory of the specified link. If false, the
returned file will left as specified in the shortcut link.
public static boolean isCygwinSymlink(java.io.File link)
link
- A file possibly representing a Cygwin symbolic link
public static java.io.File getCygwinSymlinkFile(java.io.File link, boolean makeAbsolute)
link
- The Cygwin symbolic link file.makeAbsolute
- If true and the link is not absolute, the returned file will
be made relative to the parent directory of the specified link. If false, the
returned file will left as specified in the Cygwin symbolic link.
public static boolean canExecute(java.io.File f)
f
- The file to test
public static boolean canRead(java.io.File f)
f
- The file to test
public static boolean canWrite(java.io.File f)
f
- The file to test
public static java.io.File findFile(java.io.File parentDir, java.lang.String[] fileNames)
parentDir
- The parent directoryfileNames
- The list of files to search for
public static java.io.File[] getFilesystemRoots()
public static java.io.File[] getDirectories(java.io.File dir)
dir
- The parent directory
public static java.io.File[] getFiles(java.io.File dir)
dir
- The parent directory
public static java.io.File[] getFiles(java.io.File dir, java.util.Set<java.lang.String> extnSet, boolean recursiveDecent)
dir
- The parent directoryextnSet
- The set of extensions to look forrecursiveDecent
- True to descend all directories looking for matching files
public static java.io.File[] getFiles(java.io.File dir, java.lang.String[] extnList, boolean recursiveDecent)
dir
- The parent directoryextnList
- The list of extensions to look forrecursiveDecent
- True to descend all directories looking for matching files
public static java.io.File[] getFiles(java.io.File dir, java.util.regex.Pattern regex, boolean recursiveDecent)
dir
- The parent directoryregex
- A regular expression (Pattern) that the file names must matchrecursiveDecent
- True to descend all directories looking for matching files
public static java.io.File[] getFiles(java.io.File dir, java.lang.String fileGlob, boolean recursiveDecent)
dir
- The parent directoryfileGlob
- A file glob expression that the file names must matchrecursiveDecent
- True to descend all directories looking for matching files
public static boolean traverseAllFiles(java.io.File dir, java.io.FileFilter filter)
dir
- The parent directoryfilter
- The callback filter
public static java.lang.String getHash_MD5(java.io.File file)
file
- The file
public static java.lang.String getHash_MD5(byte[] data)
data
- The data bytes
public static java.lang.String getHash_SHA1(java.io.File file)
file
- The file
public static java.lang.String getHash_SHA1(byte[] data)
data
- The data bytes
public static java.lang.String getHash_SHA256(java.io.File file)
file
- The file
public static java.lang.String getHash_SHA256(byte[] data)
data
- The data bytes
public static boolean supportsFilesGetAttribute()
public static java.lang.Object getFilePath(java.io.File file) throws java.lang.UnsupportedOperationException, java.lang.IllegalArgumentException
java.lang.UnsupportedOperationException
java.lang.IllegalArgumentException
public static java.lang.Object getAttribute(java.io.File file, java.lang.String attr) throws java.lang.UnsupportedOperationException, java.lang.IllegalArgumentException, java.io.IOException, java.lang.SecurityException
java.lang.UnsupportedOperationException
java.lang.IllegalArgumentException
java.io.IOException
java.lang.SecurityException
public static boolean isSymbolicLink(java.io.File file) throws java.lang.UnsupportedOperationException
java.lang.UnsupportedOperationException
public static java.lang.String getUserOwner(java.io.File file)
public static java.lang.String getGroupOwner(java.io.File file)
public static void main(java.lang.String[] argv)
argv
- The Command-line args
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |