Package com.sun.enterprise.util.io
Class FileUtils
java.lang.Object
com.sun.enterprise.util.io.FileUtils
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendText(String fileName, String line) Appends the given line at the end of given text file.static voidappendText(String fileName, StringBuffer buffer) static voidCopies a file.static voidcopy(InputStream in, FileOutputStream out, long size) static voidcopy(InputStream in, OutputStream os, long size) static voidCopies a file.static voidThis method is used to copy a given file to another file using the buffer sixe specifiedstatic voidCopies the entire tree to a new location.static voidcopyWithoutClose(InputStream in, FileOutputStream out, long size) static FilecreateTempFile(File directory) static booleandeleteFile(File f) Delete a file.static booleanWrapper for File.delete This version will return true if the file does not exist when the method returns.static voidDelete a file.static booleanDelete a file.static voidensureWritableDir(File dir) Result: existing writable directory given in parameter ORIllegalStateException.static File[]findFilesInDir(File dir, String regexp) Find files matching the regular expression in the given directorystatic StringformatFileCollection(Collection<File> files) Creates a String listing the absolute paths of files, separated by the platform's line separator.getAllFilesAndDirectoriesUnder(File directory) getAllFilesUnder(File directory, FilenameFilter filenameFilter) Return a set of all the files (File objects) under the directory specified, with relative pathnames filtered with the filename filter (can be null for all files).getAllFilesUnder(File directory, FilenameFilter filenameFilter, boolean relativize) static FilegetDirectory(File f) static Stringstatic FilegetManagedFile(String file, File dir) If the path dir/file does not exist, look for it in the classpath.static FileThe method, java.io.File.getParentFile() does not necessarily do what you would think it does.static booleanhasExtension(File f, String ext) static booleanhasExtension(String filename, String ext) static booleanhasExtensionIgnoreCase(File f, String ext) static booleanhasExtensionIgnoreCase(String filename, String ext) static booleanisFriendlyFilename(String filename) static booleanstatic booleanstatic booleanisLegalFilename(String filename) static booleanstatic booleanstatic voidstatic File[]listAllFiles(File dirName, String ext) Returns an array of abstract pathnames that matches with the given file extension.static File[]static File[]listFiles(File f, FileFilter ff) static File[]listFiles(File f, FilenameFilter fnf) static StringmakeForwardSlashes(String inputStr) Returns a String with uniform slashes such that all the occurances of '\\' are replaced with '/'.static StringmakeFriendlyFilename(String filename) static StringmakeFriendlyFilenameExtension(String filename) static StringmakeFriendlyFilenameNoExtension(String filename) static StringmakeLegalFilename(String filename) static StringmakeLegalNoBlankFileName(String filename) static booleanmkdirsMaybe(File f) Wrapper for File.mkdirs This version will return true if the directory exists when the method returns.static FileOutputStreamopenFileOutputStream(File out) Opens a stream to the specified output file, retrying if necessary.static StringGiven a string (typically a path), quote the string such that spaces are protected from interpretation by a Unix or Windows command shell.static StringreadSmallFile(File file) static StringreadSmallFile(String fileName) A utility routine to read a text file efficiently and return the contents as a String.static Filerelativize(File parent, File child) Given a directory and a fully-qualified file somewhere under that directory, return the portion of the child that is relative to the parent.static booleanrenameFile(File fromFile, File toFile) Rename, running gc on Windows if needed to try to force open streams to close.static byte[]resourceToBytes(String resourceName) Read in the given resourceName as a resource, and convert to a byte arraystatic StringresourceToString(String resourceName) Read in the given resourceName as a resource, and convert to a Stringstatic StringrevertFriendlyFilename(String filename) static StringrevertFriendlyFilenameExtension(String filename) static Filestatic Stringstatic booleanstatic booleanstatic booleanstatic booleanReturns a list of abstract pathnames that matches with the given file extension.static booleanDeletes a directory and its contents.static booleanwhack(File parent, Collection<File> undeletedFiles) Deletes a directory and its contents.static voidwriteStringToFile(String s, File f) Write the String to a file.
-
Field Details
-
USER_HOME
Current user's home directory resolved from the system property user.home
-
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
getParentFile
The method, java.io.File.getParentFile() does not necessarily do what you would think it does. What it really does is to simply chop off the final element in the path and return what is left-over. E.g. if the file is /foo/. then the "parent" that is returned is /foo which is probably not what you expected. This method really returns the parent directory - or null if there is none.- Parameters:
f-- Returns:
-
ensureWritableDir
Result: existing writable directory given in parameter ORIllegalStateException.- Parameters:
dir-- Throws:
IllegalStateException- The exception should not be catched, because it means that the client can have unpredictable issues.
-
mkdirsMaybe
Wrapper for File.mkdirs This version will return true if the directory exists when the method returns. Unlike File.mkdirs which returns false if the directory already exists.- Parameters:
f- The file pointing to the directory to be created- Returns:
-
deleteFileMaybe
Wrapper for File.delete This version will return true if the file does not exist when the method returns. Unlike File.delete which returns false if the file does not exist.- Parameters:
f- The file to be deleted- Returns:
-
listFiles
-
listFiles
-
listFiles
-
safeIsDirectory
-
safeIsRealDirectory
-
safeIsRealDirectory
-
safeIsDirectory
-
safeGetCanonicalPath
-
safeGetCanonicalFile
-
hasExtension
-
hasExtension
-
hasExtensionIgnoreCase
-
hasExtensionIgnoreCase
-
isLegalFilename
-
isFriendlyFilename
-
makeLegalFilename
-
makeLegalNoBlankFileName
-
makeFriendlyFilename
-
makeFriendlyFilenameNoExtension
-
makeFriendlyFilenameExtension
-
revertFriendlyFilenameExtension
-
revertFriendlyFilename
-
liquidate
-
isJar
-
isZip
-
isJar
-
isZip
-
whack
Deletes a directory and its contents. If this method encounters a symbolic link in the subtree below "parent" then it deletes the link but not any of the files pointed to by the link. Note that whack will delete files if a symbolic link appears in the path above the specified parent directory in the path.- Parameters:
parent- the File at the top of the subtree to delete- Returns:
- success or failure of deleting the directory
-
whack
Deletes a directory and its contents. If this method encounters a symbolic link in the subtree below "parent" then it deletes the link but not any of the files pointed to by the link. Note that whack will delete files if a symbolic link appears in the path above the specified parent directory in the path.- Parameters:
parent- the File at the top of the subtree to delete- Returns:
- success or failure of deleting the directory
-
deleteFileNowOrLater
Delete a file. If impossible to delete then try to delete it when the JVM exits. E.g. when Windows is using a jar in the current JVM -- you can not delete the jar until the JVM dies.- Parameters:
f- file to delete
-
deleteFileWithWaitLoop
Delete a file. Will retry every ten milliseconds for five seconds, doing a gc after each second.- Parameters:
f- file to delete- Returns:
- boolean indicating success or failure of the deletion atttempt; returns true if file is absent
-
deleteFile
Delete a file. If on Windows and the delete fails, run the gc and retry the deletion.- Parameters:
f- file to delete- Returns:
- boolean indicating success or failure of the deletion atttempt; returns true if file is absent
-
openFileOutputStream
Opens a stream to the specified output file, retrying if necessary.- Parameters:
out- the output File for which a stream is needed- Returns:
- the FileOutputStream
- Throws:
IOException- for any errors opening the stream
-
getAllFilesUnder
public static Set<File> getAllFilesUnder(File directory, FilenameFilter filenameFilter) throws IOException Return a set of all the files (File objects) under the directory specified, with relative pathnames filtered with the filename filter (can be null for all files).- Throws:
IOException
-
getAllFilesUnder
public static Set<File> getAllFilesUnder(File directory, FilenameFilter filenameFilter, boolean relativize) throws IOException - Throws:
IOException
-
getAllFilesAndDirectoriesUnder
- Throws:
IOException
-
relativize
Given a directory and a fully-qualified file somewhere under that directory, return the portion of the child that is relative to the parent. -
formatFileCollection
Creates a String listing the absolute paths of files, separated by the platform's line separator.- Parameters:
files- the Collection of File objects to be listed- Returns:
- String containing the absolute paths of the files with the line separator between them
-
getDirectory
-
createTempFile
-
listAllFiles
Returns an array of abstract pathnames that matches with the given file extension. If the given abstract pathname does not denote a directory, then this method returns null. If there is no matching file under the given directory and its sub directories, it returns null;- Parameters:
dirName- dir name under which search will beginext- file extension to look for- Returns:
- an array of abstract pathnames that matches with the extension
-
searchDir
Returns a list of abstract pathnames that matches with the given file extension. If the given abstract pathname does not denote a directory, then this method returns null. If there is no matching file under the given directory and its sub directories, it returns an empty list.- Parameters:
dirName- dir name under which search will beginext- file extension to look for- Returns:
- a list of abstract pathnames of type java.io.File that matches with the given extension
-
copy
Copies a file.- Parameters:
from- Name of file to copyto- Name of new file- Throws:
IOException- if an error while copying the content
-
copy
Copies a file.- Parameters:
fin- File to copyfout- New file- Throws:
IOException- if an error while copying the content
-
copyTree
Copies the entire tree to a new location.- Parameters:
din- File pointing at root of tree to copydout- File pointing at root of new tree- Throws:
IOException- if an error while copying the content
-
makeForwardSlashes
Returns a String with uniform slashes such that all the occurances of '\\' are replaced with '/'. In other words, the returned string will have all forward slashes. Accepts non-null strings only.- Parameters:
inputStr- non null String- Returns:
- a String which
does not contain `\\` character
-
quoteString
Given a string (typically a path), quote the string such that spaces are protected from interpretation by a Unix or Windows command shell. Note that this method does not handle quoting for all styles of special characters. Just for the basic case of strings with spaces.- Parameters:
s- input string- Returns:
- a String which is quoted to protect spaces
-
getIllegalFilenameCharacters
-
copyFile
This method is used to copy a given file to another file using the buffer sixe specified- Parameters:
fin- the source filefout- the destination file- Throws:
IOException
-
copy
- Throws:
IOException
-
copyWithoutClose
public static void copyWithoutClose(InputStream in, FileOutputStream out, long size) throws IOException - Throws:
IOException
-
copy
- Throws:
IOException
-
renameFile
Rename, running gc on Windows if needed to try to force open streams to close.- Parameters:
fromFile- to be renamedtoFile- name for the renamed file- Returns:
- boolean result of the rename attempt
-
appendText
Appends the given line at the end of given text file. If the given file does not exist, an attempt is made to create it. Note that this method can handle only text files.- Parameters:
fileName- name of the text file that needs to be appended toline- the line to append to- Throws:
RuntimeException- in case of any error - that makes it callable from a code not within try-catch. Note that NPE will be thrown if either argument is null. Note that this method is not tested with String containing characters with 2 bytes.
-
appendText
public static void appendText(String fileName, StringBuffer buffer) throws IOException, FileNotFoundException - Throws:
IOExceptionFileNotFoundException
-
readSmallFile
A utility routine to read a text file efficiently and return the contents as a String. Sometimes while reading log files of spawned processes this kind of facility is handy. Instead of opening files, coding FileReaders etc. this method could be employed. It is expected that the file to be read issmall.- Parameters:
fileName- String representing absolute path of the file- Returns:
- String representing the contents of the file, empty String for an empty file
- Throws:
IOException- if there is an i/o error.FileNotFoundException- if the file could not be found
-
readSmallFile
- Throws:
IOException
-
getManagedFile
If the path dir/file does not exist, look for it in the classpath. If found in classpath, create dir/file.- Parameters:
file- - path to look fordir- - directory where the path file should exist- Returns:
- the File representing dir/file. If that does not exist, return null.
- Throws:
IOException
-
writeStringToFile
Write the String to a file. Then make the file readable and writable. If the file already exists it will be truncated and the contents replaced with the String argument.- Parameters:
s- The String to write to the filef- The file to write the String to- Throws:
IOException- if any errors
-
findFilesInDir
Find files matching the regular expression in the given directory- Parameters:
dir- the directory to searchregexp- the regular expression pattern- Returns:
- either an array of matching File objects or an empty array. Guaranteed to never return null
-
resourceToString
Read in the given resourceName as a resource, and convert to a String- Parameters:
resourceName-- Returns:
- the contents of the resource as a String or null if absent
-
resourceToBytes
Read in the given resourceName as a resource, and convert to a byte array- Parameters:
resourceName-- Returns:
- the contents of the resource as a byte array or null if absent
-