public class ERXFileUtilities extends Object
| Constructor and Description |
|---|
ERXFileUtilities() |
| Modifier and Type | Method and Description |
|---|---|
static NSArray<File> |
arrayByAddingFilesInDirectory(File directory,
boolean recursive)
Creates a new NSArray which contains all files in the specified directory.
|
static byte[] |
bytesFromFile(File f)
Returns the byte array for a given file.
|
static byte[] |
bytesFromFile(File f,
int n)
Returns an array of the first n bytes for a given file.
|
static byte[] |
bytesFromGZippedFile(File f)
Returns the byte array for a given gzipped file.
|
static byte[] |
bytesFromInputStream(InputStream in)
Returns the byte array for a given stream.
|
static byte[] |
bytesFromInputStream(InputStream fis,
int n)
Returns an array of the first n bytes for a given input stream
|
static Charset |
charset() |
static void |
chmod(File file,
String mode)
Java wrapper for call out to chmod.
|
static void |
chmodRecursively(File dir,
String mode)
Java wrapper for call out to chmod with -R parameter for recursive processing.
|
static void |
copyFilesFromDirectory(File srcDirectory,
File dstDirectory,
boolean deleteOriginals,
boolean replaceExistingFiles,
boolean recursiveCopy,
FileFilter filter)
Copies all of the files in a given directory to another directory.
|
static void |
copyFilesFromDirectory(File srcDirectory,
File dstDirectory,
boolean deleteOriginals,
boolean recursiveCopy,
FileFilter filter)
Copies all of the files in a given directory to another directory.
|
static void |
copyFileToFile(File srcFile,
File dstFile,
boolean deleteOriginals,
boolean forceDelete)
Copies the source file to the destination.
|
static File |
createTempDir()
Creates a temporary directory.
|
static File |
createTempDir(String prefix,
String suffix)
Creates a temporary directory.
|
static String |
datePathWithRoot(String rootPath)
Returns a path containing an optional root with a directory hierarchy based on the current time
|
static boolean |
deleteDirectory(File directory)
Deletes a given directory in a recursive fashion.
|
static boolean |
deleteFile(File fileToDelete)
Deletes the given file by using the method deleteDirectory.
|
static boolean |
deleteFiles(NSArray<File> filesToDelete)
Deletes all files in array
filesToDelete by
using the method deleteDirectory. |
static void |
deleteFilesInDirectory(File directory,
boolean recurseIntoDirectories)
Deletes all of the files in a given directory with the option to
recursively delete all of the directories in the given directory.
|
static void |
deleteFilesInDirectory(File directory,
FileFilter filter,
boolean recurseIntoDirectories,
boolean removeDirectories)
Deletes all of the files in a given directory with the option to
recursively delete all of the files in the given directory.
|
static String |
fileExtension(String name)
returns the fileExtension from the specified filename
|
static String |
fileNameFromBrowserSubmittedPath(String path)
Returns the file name portion of a browser submitted path.
|
static InputStream |
inputStreamForResourceNamed(String fileName,
String frameworkName,
NSArray<String> languages)
Get the input stream from the specified Resource.
|
static long |
lastModifiedDateForFileInFramework(String fileName,
String frameworkName)
Determines the last modification date for a given file
in a framework.
|
static long |
lastModifiedDateForFileInFramework(String fileName,
String frameworkName,
NSArray<String> languages)
Determines the last modification date for a given file
in a framework.
|
static long |
length(File f)
Returns the size of the given file.
|
static void |
linkFiles(File source,
File destination,
boolean symbolic,
boolean allowUnlink,
boolean followSymbolicLinks)
Creates a symlink for a given file.
|
static File[] |
listDirectories(File baseDir,
boolean recursive)
Lists all directories in the specified directory, is desired recursive.
|
static File[] |
listFiles(File baseDir,
boolean recursive,
FileFilter filter)
Lists all files in the specified directory, if desired recursively.
|
static byte[] |
md5(File file)
Generate an MD5 hash from a file.
|
static byte[] |
md5(InputStream in)
Generate an MD5 hash from an input stream.
|
static String |
md5Hex(File file)
Generate an MD5 hash from a file.
|
static String |
md5Hex(InputStream in)
Generate an MD5 hash from an input stream.
|
static String |
pathForResourceNamed(String fileName,
String frameworkName,
NSArray<String> languages)
Determines the path of the specified Resource.
|
static URL |
pathURLForResourceNamed(String fileName,
String frameworkName,
NSArray<String> languages)
Determines the path URL of the specified Resource.
|
static Object |
readPropertyListFromFileInFramework(String fileName,
String aFrameWorkName)
Reads a file in from the file system and then parses
it as if it were a property list, using the platform's default encoding.
|
static Object |
readPropertyListFromFileInFramework(String fileName,
String aFrameWorkName,
NSArray<String> languageList)
Reads a file in from the file system for the given set
of languages and then parses the file as if it were a
property list, using the platform's default encoding.
|
static Object |
readPropertyListFromFileInFramework(String fileName,
String aFrameWorkName,
NSArray<String> languageList,
String encoding)
Reads a file in from the file system for the given set
of languages and then parses the file as if it were a
property list, using the specified encoding.
|
static Object |
readPropertyListFromFileInFramework(String fileName,
String aFrameWorkName,
String encoding)
Reads a file in from the file system and then parses
it as if it were a property list, using the specified encoding.
|
static void |
remoteCopyFile(File srcFile,
String dstHost,
String dstPath)
Copy a file across hosts using scp.
|
static void |
remoteCopyFile(String srcHost,
String srcPath,
File dstFile)
Copy a file across hosts using scp.
|
static void |
remoteCopyFile(String srcHost,
String srcPath,
String dstHost,
String dstPath)
Copy a file across hosts using scp.
|
static String |
removeFileExtension(String name)
returns the filename without its fileExtension
|
static void |
renameTo(File source,
File destination)
Moves a file from one location to another one.
|
static String |
replaceFileExtension(String path,
String newExtension)
Replaces the extension of the given file with the new extension.
|
static File |
reserveUniqueFile(File desiredFile,
boolean overwrite)
Reserves a unique file on the filesystem based on the given file name.
|
static boolean |
resourceExists(String fileName,
String frameworkName,
NSArray<String> languages)
Determines if a given resource exists.
|
static void |
setDefaultCharset(String name) |
static String |
shortenFilename(String name,
int maxLength)
shortens a filename, for example aVeryLongFileName.java -> aVer...Name.java
|
static String |
stringFromFile(File f)
Returns a string from the file using the default
encoding.
|
static String |
stringFromFile(File f,
String encoding)
Returns a string from the file using the specified
encoding.
|
static String |
stringFromGZippedFile(File f)
Returns a string from the gzipped file using the default
encoding.
|
static String |
stringFromInputStream(InputStream in)
Returns a string from the input stream using the default
encoding.
|
static String |
stringFromInputStream(InputStream in,
String encoding)
Returns a string from the input stream using the specified
encoding.
|
static String |
stringFromURL(URL url)
Returns the String from the contents of the given URL.
|
static void |
stringToFile(String s,
File f)
Writes the contents of
s to f
using the platform's default encoding. |
static void |
stringToFile(String s,
File f,
String encoding)
Writes the contents of
s to f
using specified encoding. |
static void |
stringToGZippedFile(String s,
File f) |
static File |
unzipFile(File f,
File destination)
Decompresses the specified zipfile.
|
static URL |
URLFromFile(File file)
Create an URL for a given file.
|
static URL |
URLFromPath(String fileName)
Create an URL for a given path.
|
static void |
writeInputStreamToFile(InputStream stream,
File file)
Writes the contents of an InputStream to a specified file.
|
static void |
writeInputStreamToGZippedFile(InputStream stream,
File file) |
static void |
writeInputStreamToOutputStream(InputStream in,
boolean closeInputStream,
OutputStream out,
boolean closeOutputStream)
Copies the contents of the input stream to the given output stream.
|
static void |
writeInputStreamToOutputStream(InputStream in,
OutputStream out)
Copies the contents of the input stream to the given output stream.
|
static File |
writeInputStreamToTempFile(InputStream stream)
Writes the contents of an InputStream to a temporary file.
|
static File |
writeInputStreamToTempFile(InputStream stream,
String prefix,
String suffix)
Writes the contents of an InputStream to a temporary file.
|
static void |
writeUrlToTempFile(String url,
File file)
Copies the contents of the given URL to a file.
|
static File |
writeUrlToTempFile(String url,
String prefix,
String suffix)
Copies the contents of the given URL to a temporary file.
|
static void |
writeUrlToTempFile(URL url,
File file)
Copies the contents of the given URL to a file.
|
static File |
writeUrlToTempFile(URL url,
String prefix,
String suffix)
Copies the contents of the given URL to a temporary file.
|
static File |
zipFile(File f,
boolean absolutePaths,
boolean deleteOriginal,
boolean forceDelete)
Compresses a given File with zip.
|
static File |
zipFile(File f,
boolean absolutePaths,
boolean deleteOriginal,
boolean forceDelete,
int level)
Compresses a given File with zip.
|
public static Charset charset()
public static void setDefaultCharset(String name)
public static File writeUrlToTempFile(String url, String prefix, String suffix) throws IOException
url - the URL to copy fromprefix - the temporary file prefixsuffix - the temporary file suffix (if null, the extension from the URL is used)IOException - if the copy failspublic static File writeUrlToTempFile(URL url, String prefix, String suffix) throws IOException
url - the URL to copy fromprefix - the temporary file prefixsuffix - the temporary file suffix (if null, the extension from the URL is used)IOException - if the copy failspublic static void writeUrlToTempFile(String url, File file) throws IOException
url - the URL to copy fromfile - the File to write toIOException - if the copy failspublic static void writeUrlToTempFile(URL url, File file) throws IOException
url - the URL to copy fromfile - the File to write toIOException - if the copy failspublic static byte[] bytesFromInputStream(InputStream in) throws IOException
in - stream to get the bytes fromIOException - if things go wrongpublic static String stringFromInputStream(InputStream in, String encoding) throws IOException
in - stream to readencoding - to be used, null will use the defaultIOException - if things go wrongpublic static String stringFromInputStream(InputStream in) throws IOException
in - stream to readIOException - if things go wrongpublic static String stringFromURL(URL url) throws IOException
url - the URL to read fromIOException - if an error occurspublic static byte[] bytesFromGZippedFile(File f) throws IOException
f - file to get the bytes fromIOException - if things go wrongpublic static byte[] bytesFromFile(File f) throws IOException
f - file to get the bytes fromIOException - if things go wrongpublic static byte[] bytesFromFile(File f, int n) throws IOException
f - file to get the bytes fromn - number of bytes to read from input fileIOException - if things go wrongpublic static byte[] bytesFromInputStream(InputStream fis, int n) throws IOException
fis - inputstream to get the bytes fromn - number of bytes to read from input streamIOException - if things go wrongpublic static File writeInputStreamToTempFile(InputStream stream) throws IOException
stream - to pull data fromIOException - if things go wrongpublic static File writeInputStreamToTempFile(InputStream stream, String prefix, String suffix) throws IOException
stream - to pull data fromprefix - the filename prefix of the temp filesuffix - the filename suffix of the temp fileIOException - if things go wrongpublic static void writeInputStreamToFile(InputStream stream, File file) throws IOException
file - to write tostream - to pull data fromIOException - if things go wrongpublic static void writeInputStreamToGZippedFile(InputStream stream, File file) throws IOException
IOExceptionpublic static void writeInputStreamToOutputStream(InputStream in, OutputStream out) throws IOException
in - the input stream to copy fromout - the output stream to copy toIOException - if there is any failurepublic static void writeInputStreamToOutputStream(InputStream in, boolean closeInputStream, OutputStream out, boolean closeOutputStream) throws IOException
in - the input stream to copy fromcloseInputStream - if true, the input stream will be closedout - the output stream to copy tocloseOutputStream - if true, the output stream will be closedIOException - if there is any failurepublic static void stringToGZippedFile(String s, File f) throws IOException
IOExceptionpublic static void stringToFile(String s, File f) throws IOException
s to f
using the platform's default encoding.s - the string to be written to filef - the destination fileIOException - if things go wrongpublic static void stringToFile(String s, File f, String encoding) throws IOException
s to f
using specified encoding.s - the string to be written to filef - the destination fileencoding - the desired encodingIOException - if things go wrongpublic static void remoteCopyFile(String srcHost, String srcPath, String dstHost, String dstPath) throws IOException
srcHost - host to send from (null if file is local)srcPath - path on srcHost to read fromdstHost - host to send to (null if file is local)dstPath - path on srcHost to write toIOException - if things go wrongpublic static void remoteCopyFile(File srcFile, String dstHost, String dstPath) throws IOException
srcFile - local file to senddstHost - host to send to (null if file is local)dstPath - path on srcHost to write toIOException - if things go wrongpublic static void remoteCopyFile(String srcHost, String srcPath, File dstFile) throws IOException
srcHost - host to send from (null if file is local)srcPath - path on srcHost to read fromdstFile - local file to write toIOException - if things go wrongpublic static String stringFromGZippedFile(File f) throws IOException
f - file to readIOException - if things go wrongpublic static String stringFromFile(File f) throws IOException
f - file to readIOException - if things go wrongpublic static String stringFromFile(File f, String encoding) throws IOException
f - file to readencoding - to be used, null will use the defaultIOException - if things go wrongpublic static String pathForResourceNamed(String fileName, String frameworkName, NSArray<String> languages)
fileName - name of the fileframeworkName - name of the framework, null or "app"
for the application bundlelanguages - array of languages to get localized resource or nullpublic static boolean resourceExists(String fileName, String frameworkName, NSArray<String> languages)
fileName - name of the fileframeworkName - name of the framework, null or "app"
for the application bundlelanguages - array of languages to get localized resource or nullpublic static InputStream inputStreamForResourceNamed(String fileName, String frameworkName, NSArray<String> languages)
fileName - name of the fileframeworkName - name of the framework, null or "app"
for the application bundlelanguages - array of languages to get localized resource or nullpublic static String datePathWithRoot(String rootPath)
rootPath - Root of the path before the above the date directoriespublic static URL pathURLForResourceNamed(String fileName, String frameworkName, NSArray<String> languages)
fileName - name of the fileframeworkName - name of the framework, null or "app"
for the application bundlelanguages - array of languages to get localized resource or nullpublic static URL URLFromFile(File file)
file - name of the filepublic static URL URLFromPath(String fileName)
fileName - path of the filepublic static long lastModifiedDateForFileInFramework(String fileName, String frameworkName)
fileName - name of the fileframeworkName - name of the framework, null or "app"
for the application bundlelastModified method off of the
file objectpublic static long lastModifiedDateForFileInFramework(String fileName, String frameworkName, NSArray<String> languages)
fileName - name of the fileframeworkName - name of the framework, null or "app"
for the application bundlelanguages - array of languages to get localized resource or nulllastModified method off of the file objectpublic static Object readPropertyListFromFileInFramework(String fileName, String aFrameWorkName)
fileName - name of the fileaFrameWorkName - name of the framework, null or
'app' for the application bundle.public static Object readPropertyListFromFileInFramework(String fileName, String aFrameWorkName, String encoding)
fileName - name of the fileaFrameWorkName - name of the framework, null or
'app' for the application bundle.encoding - the encoding used with fileNamepublic static Object readPropertyListFromFileInFramework(String fileName, String aFrameWorkName, NSArray<String> languageList)
fileName - name of the fileaFrameWorkName - name of the framework, null or
'app' for the application bundle.languageList - language list search orderpublic static Object readPropertyListFromFileInFramework(String fileName, String aFrameWorkName, NSArray<String> languageList, String encoding)
fileName - name of the fileaFrameWorkName - name of the framework, null or
'app' for the application bundle.languageList - language list search orderencoding - the encoding used with fileNamepublic static void deleteFilesInDirectory(File directory, boolean recurseIntoDirectories)
directory - to delete all of the files fromrecurseIntoDirectories - determines if the delete is recursivepublic static void deleteFilesInDirectory(File directory, FileFilter filter, boolean recurseIntoDirectories, boolean removeDirectories)
directory - to delete all of the files fromfilter - optional FileFilter to restrict what gets deleted, null to delete everythingrecurseIntoDirectories - determines if the delete is recursiveremoveDirectories - true if directories should be removed as well as files, false to only remove filespublic static boolean deleteDirectory(File directory)
directory - to be deletedpublic static void chmod(File file, String mode) throws IOException
file - the File to run chmod onmode - see the chmod man pageIOException - if things go wrongpublic static void chmodRecursively(File dir, String mode) throws IOException
dir - the File to run chmod onmode - see the chmod man pageIOException - if things go wrongpublic static void linkFiles(File source, File destination, boolean symbolic, boolean allowUnlink, boolean followSymbolicLinks) throws IOException
source - to create the link todestination - file to create the link tosymbolic - determines if a symlink should be createdallowUnlink - determines if the symlink is a hardlink which allows unlinkingfollowSymbolicLinks - If the destination is a symbolic link, follow itIOException - if the link could not be createdpublic static void copyFilesFromDirectory(File srcDirectory, File dstDirectory, boolean deleteOriginals, boolean recursiveCopy, FileFilter filter) throws IOException
srcDirectory - source directorydstDirectory - destination directorydeleteOriginals - tells if the original files, the file is deleted even if appuser has no write
rights. This is compareable to a rm -f filename instead of rm filenamerecursiveCopy - specifies if directories should be recursively copiedfilter - which restricts the files to be copiedIOException - if things go wrongpublic static void copyFilesFromDirectory(File srcDirectory, File dstDirectory, boolean deleteOriginals, boolean replaceExistingFiles, boolean recursiveCopy, FileFilter filter) throws IOException
srcDirectory - source directorydstDirectory - destination directorydeleteOriginals - tells if the original files, the file is deleted even if appuser has no write
rights. This is comparable to a rm -f filename instead of rm filenamereplaceExistingFiles - true if the destination should be overwritten if it already existsrecursiveCopy - specifies if directories should be recursively copiedfilter - which restricts the files to be copiedIOException - if things go wrongpublic static void copyFileToFile(File srcFile, File dstFile, boolean deleteOriginals, boolean forceDelete) throws IOException
dstFile if they are missing.srcFile - source filedstFile - destination file which may or may not exist already. If it exists, its contents will be overwritten.deleteOriginals - if true then srcFile will be deleted. Note that if the appuser has no write rights
on srcFile it is NOT deleted unless forceDelete is trueforceDelete - if true then missing write rights are ignored and the file is deleted.IOException - if things go wrongpublic static final File createTempDir() throws IOException
IOException - if something goes wrongpublic static final File createTempDir(String prefix, String suffix) throws IOException
prefix - prefix to use for the filenamesuffix - suffix to use for the filenameIOException - if something goes wrongpublic static NSArray<File> arrayByAddingFilesInDirectory(File directory, boolean recursive)
directory - the directory from which to add the filesrecursive - if true then files are added recursively meaning subdirectories are scanned, too.public static String replaceFileExtension(String path, String newExtension)
path - the path of the file.newExtension - the new extension.public static File unzipFile(File f, File destination) throws IOException
null
then the System Property "java.io.tmpdir" is used as destination for the
uncompressed file(s).f - The file to unzipdestination - the destination directory. If directory is null then the file will be unzipped in
java.io.tmpdir, if it does not exist, then a directory is created and if it exists but is a file
then the destination is set to the directory in which the file is located.IOException - if something goes wrong like not able to create a directoryZipException - if an extracted file would be placed outside of the destination directorypublic static File zipFile(File f, boolean absolutePaths, boolean deleteOriginal, boolean forceDelete) throws IOException
f - the file to zip, either a file or a directoryabsolutePaths - if true then the files are added with absolute pathsdeleteOriginal - if true then the original file is deletedforceDelete - if true then the original is deleted even if the file is read onlyIOException - if something goes wrongpublic static File zipFile(File f, boolean absolutePaths, boolean deleteOriginal, boolean forceDelete, int level) throws IOException
f - the file to zip, either a file or a directoryabsolutePaths - if true then the files are added with absolute pathsdeleteOriginal - if true then the original file is deletedforceDelete - if true then the original is deleted even if the file is read onlylevel - the compression level (0-9)IOException - if something goes wrongpublic static byte[] md5(File file) throws IOException
file - the file to sumIOException - if file could not be readpublic static byte[] md5(InputStream in) throws IOException
in - the input stream to sumIOException - if the input stream could not be readpublic static String md5Hex(File file) throws IOException
file - the file to sumIOException - if the file could not be readpublic static String md5Hex(InputStream in) throws IOException
in - the input stream to sumIOException - if the input stream could not be readpublic static long length(File f)
f points
to a directory the size of all its children will be computed.f - file to get the size ofpublic static String shortenFilename(String name, int maxLength)
name - the name to modifymaxLength - the maximum length of the name.
maxLength values under 4 have no effect, the returned string is
always a....javapublic static String removeFileExtension(String name)
name - the name of the filepublic static String fileExtension(String name)
name - the name of the filepublic static boolean deleteFiles(NSArray<File> filesToDelete)
filesToDelete by
using the method deleteDirectory.filesToDelete - array of files to deletetrue if all file have been deleted,
false otherwisepublic static boolean deleteFile(File fileToDelete)
fileToDelete - file to deletetrue if file has been deleted,
false otherwisepublic static File[] listDirectories(File baseDir, boolean recursive)
baseDir - the dir from which to list the child directoriesrecursive - if true this methods works recursivelypublic static File[] listFiles(File baseDir, boolean recursive, FileFilter filter)
baseDir - the dir from which to list the child filesrecursive - if true this method works recursivelyfilter - filter to match the files against. If null, all files will be included.public static void renameTo(File source, File destination) throws IOException
source - the file to movedestination - the destination to move the source toIOException - if things go wrongpublic static String fileNameFromBrowserSubmittedPath(String path)
path - the full path from the browserpublic static File reserveUniqueFile(File desiredFile, boolean overwrite) throws IOException
desiredFile - the desired destination file to writeoverwrite - if true, this will immediately return desiredFileIOException - if the file cannot be createdCopyright © 2002 – 2020 Project Wonder.