Project Wonder 5.0

er.extensions.foundation
Class ERXFileUtilities

java.lang.Object
  extended by er.extensions.foundation.ERXFileUtilities

public class ERXFileUtilities
extends java.lang.Object

Collection of handy {java.io.File} utilities.


Field Summary
static org.apache.log4j.Logger log
          logging support
 
Constructor Summary
ERXFileUtilities()
           
 
Method Summary
static com.webobjects.foundation.NSArray<java.io.File> arrayByAddingFilesInDirectory(java.io.File directory, boolean recursive)
          Creates a new NSArray which contains all files in the specified directory.
static byte[] bytesFromFile(java.io.File f)
          Returns the byte array for a given file.
static byte[] bytesFromFile(java.io.File f, int n)
          Returns an array of the first n bytes for a given file.
static byte[] bytesFromGZippedFile(java.io.File f)
          Returns the byte array for a given gzipped file.
static byte[] bytesFromInputStream(java.io.InputStream in)
          Returns the byte array for a given stream.
static byte[] bytesFromInputStream(java.io.InputStream fis, int n)
          Returns an array of the first n bytes for a given input stream
static void chmod(java.io.File file, java.lang.String mode)
          Java wrapper for call out to chmod.
static void chmodRecursively(java.io.File dir, java.lang.String mode)
          Java wrapper for call out to chmod with -R parameter for recursive processing.
static void copyFilesFromDirectory(java.io.File srcDirectory, java.io.File dstDirectory, boolean deleteOriginals, boolean replaceExistingFiles, boolean recursiveCopy, java.io.FileFilter filter)
          Copies all of the files in a given directory to another directory.
static void copyFilesFromDirectory(java.io.File srcDirectory, java.io.File dstDirectory, boolean deleteOriginals, boolean recursiveCopy, java.io.FileFilter filter)
          Copies all of the files in a given directory to another directory.
static void copyFileToFile(java.io.File srcFile, java.io.File dstFile, boolean deleteOriginals, boolean forceDelete)
          Copys the source file to the destination
static java.io.File createTempDir()
          Creates a temporary directory.
static java.io.File createTempDir(java.lang.String prefix, java.lang.String suffix)
          Creates a temporary directory.
static java.lang.String datePathWithRoot(java.lang.String rootPath)
          Returns a path containing an optional root with a directory hierarchy based on the current time
static boolean deleteDirectory(java.io.File directory)
          Deletes a given directory in a recursive fashion.
static boolean deleteFile(java.io.File fileToDelete)
           
static boolean deleteFiles(com.webobjects.foundation.NSMutableArray filesToDelete)
          Deletes all files in filesToDelete uses the methdo deleteDirectory
static void deleteFilesInDirectory(java.io.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(java.io.File directory, java.io.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 java.lang.String fileExtension(java.lang.String name)
          returns the fileExtension from the specified filename
static java.lang.String fileNameFromBrowserSubmittedPath(java.lang.String path)
          Returns the file name portion of a browser submitted path.
static java.io.InputStream inputStreamForResourceNamed(java.lang.String fileName, java.lang.String frameworkName, com.webobjects.foundation.NSArray languages)
          Get the input stream from the specified Resource.
static long lastModifiedDateForFileInFramework(java.lang.String fileName, java.lang.String frameworkName)
          Determines the last modification date for a given file in a framework.
static long length(java.io.File f)
           
static void linkFiles(java.io.File source, java.io.File destination, boolean symbolic, boolean allowUnlink, boolean followSymbolicLinks)
          Creates a symlink for a given file.
static java.io.File[] listDirectories(java.io.File baseDir, boolean recursive)
          Lists all directories in the specified directory, is desired recursive.
static java.io.File[] listFiles(java.io.File baseDir, boolean recursive, java.io.FileFilter filter)
          Lists all files in the specified directory, if desired recursively.
static byte[] md5(java.io.File file)
          Generate an MD5 hash from a file.
static byte[] md5(java.io.InputStream in)
          Generate an MD5 hash from an input stream.
static java.lang.String md5Hex(java.io.File file)
          Generate an MD5 hash from a file.
static java.lang.String md5Hex(java.io.InputStream in)
          Generate an MD5 hash from an input stream.
static java.lang.String pathForResourceNamed(java.lang.String fileName, java.lang.String frameworkName, com.webobjects.foundation.NSArray languages)
          Determines the path of the specified Resource.
static java.net.URL pathURLForResourceNamed(java.lang.String fileName, java.lang.String frameworkName, com.webobjects.foundation.NSArray languages)
          Determines the path URL of the specified Resource.
static java.lang.Object readPropertyListFromFileInFramework(java.lang.String fileName, java.lang.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 java.lang.Object readPropertyListFromFileInFramework(java.lang.String fileName, java.lang.String aFrameWorkName, com.webobjects.foundation.NSArray 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 java.lang.Object readPropertyListFromFileInFramework(java.lang.String fileName, java.lang.String aFrameWorkName, com.webobjects.foundation.NSArray languageList, java.lang.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 java.lang.Object readPropertyListFromFileInFramework(java.lang.String fileName, java.lang.String aFrameWorkName, java.lang.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(java.io.File srcFile, java.lang.String dstHost, java.lang.String dstPath)
          Copy a file across hosts using scp.
static void remoteCopyFile(java.lang.String srcHost, java.lang.String srcPath, java.io.File dstFile)
          Copy a file across hosts using scp.
static void remoteCopyFile(java.lang.String srcHost, java.lang.String srcPath, java.lang.String dstHost, java.lang.String dstPath)
          Copy a file across hosts using scp.
static java.lang.String removeFileExtension(java.lang.String name)
          returns the filename without its fileExtension
static void renameTo(java.io.File source, java.io.File destination)
          moves a file from one location to another one.
static java.lang.String replaceFileExtension(java.lang.String path, java.lang.String newExtension)
          Replaces the extension of the given file with the new extension.
static java.io.File reserveUniqueFile(java.io.File desiredFile, boolean overwrite)
          Reserves a unique file on the filesystem based on the given file name.
static boolean resourceExists(java.lang.String fileName, java.lang.String frameworkName, com.webobjects.foundation.NSArray languages)
          Determines if a given resource exists.
static java.lang.String shortenFilename(java.lang.String name, int maxLength)
          shortens a filename, for example aVeryLongFileName.java -> aVer...Name.java
static java.lang.String stringFromFile(java.io.File f)
          Returns a string from the file using the default encoding.
static java.lang.String stringFromFile(java.io.File f, java.lang.String encoding)
          Returns a string from the file using the specified encoding.
static java.lang.String stringFromGZippedFile(java.io.File f)
          Returns a string from the gzipped file using the default encoding.
static java.lang.String stringFromInputStream(java.io.InputStream in)
          Returns a string from the input stream using the default encoding.
static java.lang.String stringFromInputStream(java.io.InputStream in, java.lang.String encoding)
          Returns a string from the input stream using the specified encoding.
static java.lang.String stringFromURL(java.net.URL url)
          Returns the String from the contents of the given URL.
static void stringToFile(java.lang.String s, java.io.File f)
          Writes the contents of s to f using the platform's default encoding.
static void stringToFile(java.lang.String s, java.io.File f, java.lang.String encoding)
          Writes the contents of s to f using specified encoding.
static void stringToGZippedFile(java.lang.String s, java.io.File f)
           
static java.io.File unzipFile(java.io.File f, java.io.File destination)
          Decompresses the specified zipfile.
static java.net.URL URLFromFile(java.io.File file)
          Create an URL for a given file.
static java.net.URL URLFromPath(java.lang.String fileName)
          Create an URL for a given path.
static void writeInputStreamToFile(java.io.File f, java.io.InputStream is)
          Deprecated. use writeInputStreamToFile(InputStream is, File f) instead
static void writeInputStreamToFile(java.io.InputStream stream, java.io.File file)
          Writes the contents of an InputStream to a specified file.
static void writeInputStreamToGZippedFile(java.io.InputStream stream, java.io.File file)
           
static void writeInputStreamToOutputStream(java.io.InputStream in, boolean closeInputStream, java.io.OutputStream out, boolean closeOutputStream)
          Copies the contents of the input stream to the given output stream.
static void writeInputStreamToOutputStream(java.io.InputStream in, java.io.OutputStream out)
          Copies the contents of the input stream to the given output stream.
static java.io.File writeInputStreamToTempFile(java.io.InputStream stream)
          Writes the contents of an InputStream to a temporary file.
static java.io.File writeInputStreamToTempFile(java.io.InputStream stream, java.lang.String prefix, java.lang.String suffix)
          Writes the contents of an InputStream to a temporary file.
static void writeUrlToTempFile(java.lang.String url, java.io.File file)
          Copies the contents of the given URL to a file.
static java.io.File writeUrlToTempFile(java.lang.String url, java.lang.String prefix, java.lang.String suffix)
          Copies the contents of the given URL to a temporary file.
static void writeUrlToTempFile(java.net.URL url, java.io.File file)
          Copies the contents of the given URL to a file.
static java.io.File writeUrlToTempFile(java.net.URL url, java.lang.String prefix, java.lang.String suffix)
          Copies the contents of the given URL to a temporary file.
static java.io.File zipFile(java.io.File f, boolean absolutePaths, boolean deleteOriginal, boolean forceDelete)
          zips a given File.
static java.io.File zipFile(java.io.File f, boolean absolutePaths, boolean deleteOriginal, boolean forceDelete, int level)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

public static final org.apache.log4j.Logger log
logging support

Constructor Detail

ERXFileUtilities

public ERXFileUtilities()
Method Detail

writeUrlToTempFile

public static java.io.File writeUrlToTempFile(java.lang.String url,
                                              java.lang.String prefix,
                                              java.lang.String suffix)
                                       throws java.io.IOException
Copies the contents of the given URL to a temporary file.

Parameters:
url - the URL to copy from
prefix - the temporary file prefix
suffix - the temporary file suffix (if null, the extension from the URL is used)
Returns:
the temporary file
Throws:
java.io.IOException - if the copy fails

writeUrlToTempFile

public static java.io.File writeUrlToTempFile(java.net.URL url,
                                              java.lang.String prefix,
                                              java.lang.String suffix)
                                       throws java.io.IOException
Copies the contents of the given URL to a temporary file.

Parameters:
url - the URL to copy from
prefix - the temporary file prefix
suffix - the temporary file suffix (if null, the extension from the URL is used)
Returns:
the temporary file
Throws:
java.io.IOException - if the copy fails

writeUrlToTempFile

public static void writeUrlToTempFile(java.lang.String url,
                                      java.io.File file)
                               throws java.io.IOException
Copies the contents of the given URL to a file.

Parameters:
url - the URL to copy from
file - the File to write to
Throws:
java.io.IOException - if the copy fails

writeUrlToTempFile

public static void writeUrlToTempFile(java.net.URL url,
                                      java.io.File file)
                               throws java.io.IOException
Copies the contents of the given URL to a file.

Parameters:
url - the URL to copy from
file - the File to write to
Throws:
java.io.IOException - if the copy fails

bytesFromInputStream

public static byte[] bytesFromInputStream(java.io.InputStream in)
                                   throws java.io.IOException
Returns the byte array for a given stream.

Parameters:
in - stream to get the bytes from
Returns:
byte array of the stream.
Throws:
java.io.IOException - if things go wrong

stringFromInputStream

public static java.lang.String stringFromInputStream(java.io.InputStream in,
                                                     java.lang.String encoding)
                                              throws java.io.IOException
Returns a string from the input stream using the specified encoding.

Parameters:
in - stream to read
encoding - to be used, null will use the default
Returns:
string representation of the stream.
Throws:
java.io.IOException

stringFromInputStream

public static java.lang.String stringFromInputStream(java.io.InputStream in)
                                              throws java.io.IOException
Returns a string from the input stream using the default encoding.

Parameters:
in - stream to read
Returns:
string representation of the stream.
Throws:
java.io.IOException

stringFromURL

public static java.lang.String stringFromURL(java.net.URL url)
                                      throws java.io.IOException
Returns the String from the contents of the given URL.

Parameters:
url - the URL to read from
Returns:
the String contents of the URL
Throws:
java.io.IOException - if an error occurs

bytesFromGZippedFile

public static byte[] bytesFromGZippedFile(java.io.File f)
                                   throws java.io.IOException
Returns the byte array for a given gzipped file.

Parameters:
f - file to get the bytes from
Returns:
byte array of the file.
Throws:
java.io.IOException - if things go wrong

bytesFromFile

public static byte[] bytesFromFile(java.io.File f)
                            throws java.io.IOException
Returns the byte array for a given file.

Parameters:
f - file to get the bytes from
Returns:
byte array of the file.
Throws:
java.io.IOException - if things go wrong

bytesFromFile

public static byte[] bytesFromFile(java.io.File f,
                                   int n)
                            throws java.io.IOException
Returns an array of the first n bytes for a given file.

Parameters:
f - file to get the bytes from
n - number of bytes to read from input file
Returns:
byte array of the first n bytes from the file.
Throws:
java.io.IOException - if things go wrong

bytesFromInputStream

public static byte[] bytesFromInputStream(java.io.InputStream fis,
                                          int n)
                                   throws java.io.IOException
Returns an array of the first n bytes for a given input stream

Parameters:
fis - inputstream to get the bytes from
n - number of bytes to read from input stream
Returns:
byte array of the first n bytes from the file.
Throws:
java.io.IOException - if things go wrong

writeInputStreamToFile

public static void writeInputStreamToFile(java.io.File f,
                                          java.io.InputStream is)
                                   throws java.io.IOException
Deprecated. use writeInputStreamToFile(InputStream is, File f) instead

Throws:
java.io.IOException

writeInputStreamToTempFile

public static java.io.File writeInputStreamToTempFile(java.io.InputStream stream)
                                               throws java.io.IOException
Writes the contents of an InputStream to a temporary file.

Parameters:
stream - to pull data from
Returns:
the temp file that was created
Throws:
java.io.IOException

writeInputStreamToTempFile

public static java.io.File writeInputStreamToTempFile(java.io.InputStream stream,
                                                      java.lang.String prefix,
                                                      java.lang.String suffix)
                                               throws java.io.IOException
Writes the contents of an InputStream to a temporary file.

Parameters:
stream - to pull data from
prefix - the filename prefix of the temp file
suffix - the filename suffix of the temp file
Returns:
the temp file that was created
Throws:
java.io.IOException

writeInputStreamToFile

public static void writeInputStreamToFile(java.io.InputStream stream,
                                          java.io.File file)
                                   throws java.io.IOException
Writes the contents of an InputStream to a specified file.

Parameters:
file - to write to
stream - to pull data from
Throws:
java.io.IOException

writeInputStreamToGZippedFile

public static void writeInputStreamToGZippedFile(java.io.InputStream stream,
                                                 java.io.File file)
                                          throws java.io.IOException
Throws:
java.io.IOException

writeInputStreamToOutputStream

public static void writeInputStreamToOutputStream(java.io.InputStream in,
                                                  java.io.OutputStream out)
                                           throws java.io.IOException
Copies the contents of the input stream to the given output stream. Both streams are guaranteed to be closed by the end of this method.

Parameters:
in - the input stream to copy from
out - the output stream to copy to
Throws:
java.io.IOException - if there is any failure

writeInputStreamToOutputStream

public static void writeInputStreamToOutputStream(java.io.InputStream in,
                                                  boolean closeInputStream,
                                                  java.io.OutputStream out,
                                                  boolean closeOutputStream)
                                           throws java.io.IOException
Copies the contents of the input stream to the given output stream.

Parameters:
in - the input stream to copy from
closeInputStream - if true, the input stream will be closed
out - the output stream to copy to
closeOutputStream - if true, the output stream will be closed
Throws:
java.io.IOException - if there is any failure

stringToGZippedFile

public static void stringToGZippedFile(java.lang.String s,
                                       java.io.File f)
                                throws java.io.IOException
Throws:
java.io.IOException

stringToFile

public static void stringToFile(java.lang.String s,
                                java.io.File f)
                         throws java.io.IOException
Writes the contents of s to f using the platform's default encoding.

Parameters:
s - the string to be written to file
f - the destination file
Throws:
java.io.IOException

stringToFile

public static void stringToFile(java.lang.String s,
                                java.io.File f,
                                java.lang.String encoding)
                         throws java.io.IOException
Writes the contents of s to f using specified encoding.

Parameters:
s - the string to be written to file
f - the destination file
encoding - the desired encoding
Throws:
java.io.IOException

remoteCopyFile

public static void remoteCopyFile(java.lang.String srcHost,
                                  java.lang.String srcPath,
                                  java.lang.String dstHost,
                                  java.lang.String dstPath)
                           throws java.io.IOException
Copy a file across hosts using scp.

Parameters:
srcHost - host to send from (null if file is local)
srcPath - path on srcHost to read from
dstHost - host to send to (null if file is local)
dstPath - path on srcHost to write to
Throws:
java.io.IOException

remoteCopyFile

public static void remoteCopyFile(java.io.File srcFile,
                                  java.lang.String dstHost,
                                  java.lang.String dstPath)
                           throws java.io.IOException
Copy a file across hosts using scp.

Parameters:
srcFile - local file to send
dstHost - host to send to (null if file is local)
dstPath - path on srcHost to write to
Throws:
java.io.IOException

remoteCopyFile

public static void remoteCopyFile(java.lang.String srcHost,
                                  java.lang.String srcPath,
                                  java.io.File dstFile)
                           throws java.io.IOException
Copy a file across hosts using scp.

Parameters:
srcHost - host to send from (null if file is local)
srcPath - path on srcHost to read from
dstFile - local file to write to
Throws:
java.io.IOException

stringFromGZippedFile

public static java.lang.String stringFromGZippedFile(java.io.File f)
                                              throws java.io.IOException
Returns a string from the gzipped file using the default encoding.

Parameters:
f - file to read
Returns:
string representation of that file.
Throws:
java.io.IOException

stringFromFile

public static java.lang.String stringFromFile(java.io.File f)
                                       throws java.io.IOException
Returns a string from the file using the default encoding.

Parameters:
f - file to read
Returns:
string representation of that file.
Throws:
java.io.IOException

stringFromFile

public static java.lang.String stringFromFile(java.io.File f,
                                              java.lang.String encoding)
                                       throws java.io.IOException
Returns a string from the file using the specified encoding.

Parameters:
f - file to read
encoding - to be used, null will use the default
Returns:
string representation of the file.
Throws:
java.io.IOException

pathForResourceNamed

public static java.lang.String pathForResourceNamed(java.lang.String fileName,
                                                    java.lang.String frameworkName,
                                                    com.webobjects.foundation.NSArray languages)
Determines the path of the specified Resource. This is done to get a single entry point due to the deprecation of pathForResourceNamed

Parameters:
fileName - name of the file
frameworkName - name of the framework, null or "app" for the application bundle
Returns:
the absolutePath method off of the file object

resourceExists

public static boolean resourceExists(java.lang.String fileName,
                                     java.lang.String frameworkName,
                                     com.webobjects.foundation.NSArray languages)
Determines if a given resource exists. This is done to get a single entry point due to the deprecation of pathForResourceNamed

Parameters:
fileName - name of the file
frameworkName - name of the framework, null or "app" for the application bundle
Returns:
the absolutePath method off of the file object

inputStreamForResourceNamed

public static java.io.InputStream inputStreamForResourceNamed(java.lang.String fileName,
                                                              java.lang.String frameworkName,
                                                              com.webobjects.foundation.NSArray languages)
Get the input stream from the specified Resource.

Parameters:
fileName - name of the file
frameworkName - name of the framework, null or "app" for the application bundle
Returns:
the absolutePath method off of the file object

datePathWithRoot

public static java.lang.String datePathWithRoot(java.lang.String rootPath)
Returns a path containing an optional root with a directory hierarchy based on the current time

Parameters:
rootPath - Root of the path before the above the date directories
Returns:
the path based on time.

pathURLForResourceNamed

public static java.net.URL pathURLForResourceNamed(java.lang.String fileName,
                                                   java.lang.String frameworkName,
                                                   com.webobjects.foundation.NSArray languages)
Determines the path URL of the specified Resource. This is done to get a single entry point due to the deprecation of pathForResourceNamed. In a later version this will call out to the resource managers new methods directly.

Parameters:
fileName - name of the file
frameworkName - name of the framework, null or "app" for the application bundle
Returns:
the absolutePath method off of the file object

URLFromFile

public static java.net.URL URLFromFile(java.io.File file)
Create an URL for a given file.

Parameters:
file - name of the file
Returns:
file:// URL for the given path

URLFromPath

public static java.net.URL URLFromPath(java.lang.String fileName)
Create an URL for a given path.

Parameters:
fileName - path of the file
Returns:
file:// URL for the given path

lastModifiedDateForFileInFramework

public static long lastModifiedDateForFileInFramework(java.lang.String fileName,
                                                      java.lang.String frameworkName)
Determines the last modification date for a given file in a framework. Note that this method will only test for the global resource not the localized resources.

Parameters:
fileName - name of the file
frameworkName - name of the framework, null or "app" for the application bundle
Returns:
the lastModified method off of the file object

readPropertyListFromFileInFramework

public static java.lang.Object readPropertyListFromFileInFramework(java.lang.String fileName,
                                                                   java.lang.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.

Parameters:
fileName - name of the file
aFrameWorkName - name of the framework, null or 'app' for the application bundle.
Returns:
de-serialized object from the plist formatted file specified.

readPropertyListFromFileInFramework

public static java.lang.Object readPropertyListFromFileInFramework(java.lang.String fileName,
                                                                   java.lang.String aFrameWorkName,
                                                                   java.lang.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.

Parameters:
fileName - name of the file
aFrameWorkName - name of the framework, null or 'app' for the application bundle.
encoding - the encoding used with fileName
Returns:
de-serialized object from the plist formatted file specified.

readPropertyListFromFileInFramework

public static java.lang.Object readPropertyListFromFileInFramework(java.lang.String fileName,
                                                                   java.lang.String aFrameWorkName,
                                                                   com.webobjects.foundation.NSArray 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.

Parameters:
fileName - name of the file
aFrameWorkName - name of the framework, null or 'app' for the application bundle.
languageList - language list search order
Returns:
de-serialized object from the plist formatted file specified.

readPropertyListFromFileInFramework

public static java.lang.Object readPropertyListFromFileInFramework(java.lang.String fileName,
                                                                   java.lang.String aFrameWorkName,
                                                                   com.webobjects.foundation.NSArray languageList,
                                                                   java.lang.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.

Parameters:
fileName - name of the file
aFrameWorkName - name of the framework, null or 'app' for the application bundle.
languageList - language list search order
encoding - the encoding used with fileName
Returns:
de-serialized object from the plist formatted file specified.

deleteFilesInDirectory

public static void deleteFilesInDirectory(java.io.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.

Parameters:
directory - to delete all of the files from
recurseIntoDirectories - determines if the delete is recursive

deleteFilesInDirectory

public static void deleteFilesInDirectory(java.io.File directory,
                                          java.io.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.

Parameters:
directory - to delete all of the files from
filter - optional FileFilter to restrict what gets deleted, null to delete everything
recurseIntoDirectories - determines if the delete is recursive
removeDirectories - true if directories should be removed as well as files, false to only remove files

deleteDirectory

public static boolean deleteDirectory(java.io.File directory)
Deletes a given directory in a recursive fashion.

Parameters:
directory - to be deleted
Returns:
if the directory deleted successfully

chmod

public static void chmod(java.io.File file,
                         java.lang.String mode)
                  throws java.io.IOException
Java wrapper for call out to chmod. Only works if your OS supports the chmod command.

Parameters:
file - the File to run chmod on
mode - see the chmod man page
Throws:
java.io.IOException - if things go wrong

chmodRecursively

public static void chmodRecursively(java.io.File dir,
                                    java.lang.String mode)
                             throws java.io.IOException
Java wrapper for call out to chmod with -R parameter for recursive processing. Only works if your OS supports the chmod command.

Parameters:
dir - the File to run chmod on
mode - see the chmod man page
Throws:
java.io.IOException - if things go wrong

linkFiles

public static void linkFiles(java.io.File source,
                             java.io.File destination,
                             boolean symbolic,
                             boolean allowUnlink,
                             boolean followSymbolicLinks)
                      throws java.io.IOException
Creates a symlink for a given file. Note this only works on civilized OSs which support symbolic linking.

Parameters:
source - to create the link to
destination - file to create the link to
symbolic - determines if a symlink should be created
allowUnlink - determines if the symlink is a hardlink which allows unlinking
followSymbolicLinks - If the destination is a symbolic link, follow it
Throws:
java.io.IOException

copyFilesFromDirectory

public static void copyFilesFromDirectory(java.io.File srcDirectory,
                                          java.io.File dstDirectory,
                                          boolean deleteOriginals,
                                          boolean recursiveCopy,
                                          java.io.FileFilter filter)
                                   throws java.io.FileNotFoundException,
                                          java.io.IOException
Copies all of the files in a given directory to another directory. Existing files are replaced.

Parameters:
srcDirectory - source directory
dstDirectory - destination directory
deleteOriginals - 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 filename
recursiveCopy - specifies if directories should be recursively copied
filter - which restricts the files to be copied
Throws:
java.io.FileNotFoundException
java.io.IOException

copyFilesFromDirectory

public static void copyFilesFromDirectory(java.io.File srcDirectory,
                                          java.io.File dstDirectory,
                                          boolean deleteOriginals,
                                          boolean replaceExistingFiles,
                                          boolean recursiveCopy,
                                          java.io.FileFilter filter)
                                   throws java.io.FileNotFoundException,
                                          java.io.IOException
Copies all of the files in a given directory to another directory.

Parameters:
srcDirectory - source directory
dstDirectory - destination directory
deleteOriginals - 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 filename
replaceExistingFiles - true if the destination should be overwritten if it already exists
recursiveCopy - specifies if directories should be recursively copied
filter - which restricts the files to be copied
Throws:
java.io.FileNotFoundException
java.io.IOException

copyFileToFile

public static void copyFileToFile(java.io.File srcFile,
                                  java.io.File dstFile,
                                  boolean deleteOriginals,
                                  boolean forceDelete)
                           throws java.io.FileNotFoundException,
                                  java.io.IOException
Copys the source file to the destination

Parameters:
srcFile - source file
dstFile - destination file
deleteOriginals - tells if original file will be deleted. Note that if the appuser has no write rights on the file it is NOT deleted unless force delete is true
forceDelete - if true then missing write rights are ignored and the file is deleted.
Throws:
java.io.FileNotFoundException
java.io.IOException

createTempDir

public static final java.io.File createTempDir()
                                        throws java.io.IOException
Creates a temporary directory.

Returns:
a temporary directory
Throws:
java.io.IOException - if something goes wrong

createTempDir

public static final java.io.File createTempDir(java.lang.String prefix,
                                               java.lang.String suffix)
                                        throws java.io.IOException
Creates a temporary directory.

Returns:
a temporary directory
Throws:
java.io.IOException - if something goes wrong

arrayByAddingFilesInDirectory

public static com.webobjects.foundation.NSArray<java.io.File> arrayByAddingFilesInDirectory(java.io.File directory,
                                                                                            boolean recursive)
Creates a new NSArray which contains all files in the specified directory.

Parameters:
directory - the directory from which to add the files
recursive - if true then files are added recursively meaning subdirectories are scanned, too.
Returns:
a NSArray containing the files in the directory. If the specified directory does not exist then the array is empty.

replaceFileExtension

public static java.lang.String replaceFileExtension(java.lang.String path,
                                                    java.lang.String newExtension)
Replaces the extension of the given file with the new extension.

Parameters:
path - the path of the file.
newExtension - the new extension.
Returns:
the new path.

unzipFile

public static java.io.File unzipFile(java.io.File f,
                                     java.io.File destination)
                              throws java.io.IOException
Decompresses the specified zipfile. If the file is a compressed directory, the whole subdirectory structure is created as a subdirectory with the name if the zip file minus the .zip extension from destination. All intermittent directories are also created. If destination is null then the System Property "java.io.tmpdir" is used as destination for the uncompressed file(s).

Parameters:
f - The file to unzip
destination - 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.
Returns:
the file or directory in which the zipfile was unzipped
Throws:
java.io.IOException

zipFile

public static java.io.File zipFile(java.io.File f,
                                   boolean absolutePaths,
                                   boolean deleteOriginal,
                                   boolean forceDelete)
                            throws java.io.IOException
zips a given File.

Parameters:
f - the file to zip, either a file or a directory
absolutePaths - if true then the files are added with absolute paths
deleteOriginal - if true then the original file is deleted
forceDelete - if true then the original is deleted even if the file is read only
Throws:
java.io.IOException

zipFile

public static java.io.File zipFile(java.io.File f,
                                   boolean absolutePaths,
                                   boolean deleteOriginal,
                                   boolean forceDelete,
                                   int level)
                            throws java.io.IOException
Throws:
java.io.IOException

md5

public static byte[] md5(java.io.File file)
                  throws java.io.IOException
Generate an MD5 hash from a file.

Parameters:
file - the file to sum
Returns:
the MD5 sum of the bytes in file
Throws:
java.io.IOException

md5

public static byte[] md5(java.io.InputStream in)
                  throws java.io.IOException
Generate an MD5 hash from an input stream.

Parameters:
in - the input stream to sum
Returns:
the MD5 sum of the bytes in file
Throws:
java.io.IOException

md5Hex

public static java.lang.String md5Hex(java.io.File file)
                               throws java.io.IOException
Generate an MD5 hash from a file.

Parameters:
file - the file to sum
Returns:
the hex encoded MD5 sum of the bytes in file
Throws:
java.io.IOException

md5Hex

public static java.lang.String md5Hex(java.io.InputStream in)
                               throws java.io.IOException
Generate an MD5 hash from an input stream.

Parameters:
in - the input stream to sum
Returns:
the hex encoded MD5 sum of the bytes in file
Throws:
java.io.IOException

length

public static long length(java.io.File f)

shortenFilename

public static java.lang.String shortenFilename(java.lang.String name,
                                               int maxLength)
shortens a filename, for example aVeryLongFileName.java -> aVer...Name.java

Parameters:
name - the name to modify
maxLength - the maximum length of the name. maxLength values under 4 have no effect, the returned string is always a....java
Returns:
the shortened filename

removeFileExtension

public static java.lang.String removeFileExtension(java.lang.String name)
returns the filename without its fileExtension

Parameters:
name - the name of the file
Returns:
the name of the file without the fileExtension

fileExtension

public static java.lang.String fileExtension(java.lang.String name)
returns the fileExtension from the specified filename

Parameters:
name - the name of the file
Returns:
the fileExtension

deleteFiles

public static boolean deleteFiles(com.webobjects.foundation.NSMutableArray filesToDelete)
Deletes all files in filesToDelete uses the methdo deleteDirectory

Parameters:
filesToDelete -

deleteFile

public static boolean deleteFile(java.io.File fileToDelete)

listDirectories

public static java.io.File[] listDirectories(java.io.File baseDir,
                                             boolean recursive)
Lists all directories in the specified directory, is desired recursive.

Parameters:
baseDir - the dir from which to list the child directories
recursive - if true this methods works recursively
Returns:
an array of files which are directories

listFiles

public static java.io.File[] listFiles(java.io.File baseDir,
                                       boolean recursive,
                                       java.io.FileFilter filter)
Lists all files in the specified directory, if desired recursively.

Parameters:
baseDir - the dir from which to list the child files
recursive - if true this method works recursively
filter - filter to match the files against. If null, all files will be included.
Returns:
an array of files

renameTo

public static void renameTo(java.io.File source,
                            java.io.File destination)
                     throws java.io.FileNotFoundException,
                            java.io.IOException
moves a file from one location to another one. This works different than java.io.File.renameTo as renameTo does not work across partitions

Parameters:
source -
destination -
Throws:
java.io.IOException
java.io.FileNotFoundException

fileNameFromBrowserSubmittedPath

public static java.lang.String fileNameFromBrowserSubmittedPath(java.lang.String path)
Returns the file name portion of a browser submitted path.

Parameters:
path - the full path from the browser
Returns:
the file name portion

reserveUniqueFile

public static java.io.File reserveUniqueFile(java.io.File desiredFile,
                                             boolean overwrite)
                                      throws java.io.IOException
Reserves a unique file on the filesystem based on the given file name. If the given file cannot be reserved, then "-1", "-2", etc will be appended to the filename in front of the extension until a unique file name is found. This will also ensure that the parent folder is created.

Parameters:
desiredFile - the desired destination file to write
overwrite - if true, this will immediately return desiredFile
Returns:
a unique, reserved, filename
Throws:
java.io.IOException - if the file cannot be created

Last updated: Tue, Feb 21, 2017 • 05:45 PM CET

Copyright © 2002 – 2007 Project Wonder.