Package com.sun.enterprise.util.io
Class FileUtils
- java.lang.Object
-
- com.sun.enterprise.util.io.FileUtils
-
public class FileUtils extends Object
-
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
appendText(String fileName, String line)
Appends the given line at the end of given text file.static void
appendText(String fileName, StringBuilder buffer)
static void
copy(File fin, File fout)
Copies a file.static void
copy(InputStream in, FileOutputStream out, long size)
static void
copy(InputStream in, OutputStream os, long size)
Copies from InputStream to OutputStream and does not close the streams intentionally.static void
copy(String from, String to)
Copies a file.static void
copyFile(File fin, File fout)
This method is used to copy a given file to another file using the buffer sixe specifiedstatic void
copyTree(File din, File dout)
Copies the entire tree to a new location.static void
copyWithoutClose(InputStream in, FileOutputStream out, long size)
static File
createTempFile(File directory)
static File
createTempFile(InputStream in, String prefix, String suffix)
static boolean
deleteFile(File f)
Delete a file.static boolean
deleteFileMaybe(File f)
Wrapper for File.delete This version will return true if the file does not exist when the method returns.static void
deleteFileNowOrLater(File f)
Delete a file.static boolean
deleteFileWithWaitLoop(File f)
Delete a file.static void
deleteOnExit(File f)
Deletes the provided file by registering a shutdown hook with the Java Runtime.static File[]
findFilesInDir(File dir, String regexp)
Find files matching the regular expression in the given directorystatic String
formatFileCollection(Collection<File> files)
Creates a String listing the absolute paths of files, separated by the platform's line separator.static Set<File>
getAllFilesAndDirectoriesUnder(File directory)
static Set<File>
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).static Set<File>
getAllFilesUnder(File directory, FilenameFilter filenameFilter, boolean relativize)
static File
getDirectory(File f)
static String
getIllegalFilenameCharacters()
static File
getManagedFile(String file, File dir)
If the path dir/file does not exist, look for it in the classpath.static File
getParentFile(File f)
The method, java.io.File.getParentFile() does not necessarily do what you would think it does.static boolean
hasExtension(File f, String ext)
static boolean
hasExtension(String filename, String ext)
static boolean
hasExtensionIgnoreCase(File f, String ext)
static boolean
hasExtensionIgnoreCase(String filename, String ext)
static boolean
isFriendlyFilename(String filename)
static boolean
isJar(File f)
static boolean
isJar(String filename)
static boolean
isLegalFilename(String filename)
static boolean
isZip(File f)
static boolean
isZip(String filename)
static void
liquidate(File parent)
static File[]
listAllFiles(File dirName, String ext)
Returns an array of abstract pathnames that matches with the given file extension.static File[]
listFiles(File f)
static File[]
listFiles(File f, FileFilter ff)
static File[]
listFiles(File f, FilenameFilter fnf)
static String
makeForwardSlashes(String inputStr)
Returns a String with uniform slashes such that all the occurances of '\\' are replaced with '/'.static String
makeFriendlyFilename(String filename)
static String
makeFriendlyFilenameExtension(String filename)
static String
makeFriendlyFilenameNoExtension(String filename)
static String
makeLegalFilename(String filename)
static String
makeLegalNoBlankFileName(String filename)
static boolean
mkdirsMaybe(File f)
Wrapper for File.mkdirs This version will return true if the directory exists when the method returns.static FileOutputStream
openFileOutputStream(File out)
Opens a stream to the specified output file, retrying if necessary.static String
quoteString(String s)
Given a string (typically a path), quote the string such that spaces are protected from interpretation by a Unix or Windows command shell.static String
readSmallFile(File file)
static String
readSmallFile(String fileName)
A utility routine to read a text file efficiently and return the contents as a String.static File
relativize(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 boolean
renameFile(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 String
resourceToString(String resourceName)
Read in the given resourceName as a resource, and convert to a Stringstatic String
revertFriendlyFilename(String filename)
static String
revertFriendlyFilenameExtension(String filename)
static File
safeGetCanonicalFile(File f)
static String
safeGetCanonicalPath(File f)
static boolean
safeIsDirectory(File f)
static boolean
safeIsDirectory(String s)
static boolean
safeIsRealDirectory(File f)
static boolean
safeIsRealDirectory(String s)
static List<File>
searchDir(File dirName, String ext)
Returns a list of abstract pathnames that matches with the given file extension.static void
setFileProperties()
static boolean
whack(File parent)
Deletes a directory and its contents.static boolean
whack(File parent, Collection<File> undeletedFiles)
Deletes a directory and its contents.static void
writeStringToFile(String s, File f)
Write the String to a file.
-
-
-
Method Detail
-
setFileProperties
public static void setFileProperties()
-
getParentFile
public static File getParentFile(File f)
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:
-
mkdirsMaybe
public static boolean mkdirsMaybe(File f)
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
public static boolean deleteFileMaybe(File f)
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
public static File[] listFiles(File f, FileFilter ff)
-
listFiles
public static File[] listFiles(File f, FilenameFilter fnf)
-
safeIsDirectory
public static boolean safeIsDirectory(File f)
-
safeIsRealDirectory
public static boolean safeIsRealDirectory(String s)
-
safeIsRealDirectory
public static boolean safeIsRealDirectory(File f)
-
safeIsDirectory
public static boolean safeIsDirectory(String s)
-
isLegalFilename
public static boolean isLegalFilename(String filename)
-
isFriendlyFilename
public static boolean isFriendlyFilename(String filename)
-
makeFriendlyFilenameNoExtension
public static String makeFriendlyFilenameNoExtension(String filename)
-
revertFriendlyFilenameExtension
public static String revertFriendlyFilenameExtension(String filename)
-
liquidate
public static void liquidate(File parent)
-
isJar
public static boolean isJar(String filename)
-
isZip
public static boolean isZip(String filename)
-
isJar
public static boolean isJar(File f)
-
isZip
public static boolean isZip(File f)
-
whack
public static boolean whack(File parent)
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
public static boolean whack(File parent, Collection<File> undeletedFiles)
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
public static void deleteFileNowOrLater(File f)
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
-
deleteOnExit
public static void deleteOnExit(File f)
Deletes the provided file by registering a shutdown hook with the Java Runtime. Non-empty directories will be deleted recursively.- Parameters:
f
- the file to delete on JVM shutdown
-
deleteFileWithWaitLoop
public static boolean deleteFileWithWaitLoop(File f)
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
public static boolean deleteFile(File f)
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
public static FileOutputStream openFileOutputStream(File out) throws IOException
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
public static Set<File> getAllFilesAndDirectoriesUnder(File directory) throws IOException
- Throws:
IOException
-
relativize
public static File relativize(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.
-
formatFileCollection
public static String formatFileCollection(Collection<File> files)
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
-
createTempFile
public static File createTempFile(InputStream in, String prefix, String suffix) throws IOException
- Throws:
IOException
-
listAllFiles
public static File[] listAllFiles(File dirName, String ext)
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
public static List<File> searchDir(File dirName, String ext)
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
public static void copy(String from, String to) throws IOException
Copies a file.- Parameters:
from
- Name of file to copyto
- Name of new file- Throws:
IOException
- if an error while copying the content
-
copy
public static void copy(File fin, File fout) throws IOException
Copies a file.- Parameters:
fin
- File to copyfout
- New file- Throws:
IOException
- if an error while copying the content
-
copyTree
public static void copyTree(File din, File dout) throws IOException
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
public static String makeForwardSlashes(String inputStr)
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
public static String quoteString(String s)
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
public static String getIllegalFilenameCharacters()
-
copyFile
public static void copyFile(File fin, File fout) throws IOException
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
public static void copy(InputStream in, FileOutputStream out, long size) throws IOException
- Throws:
IOException
-
copyWithoutClose
public static void copyWithoutClose(InputStream in, FileOutputStream out, long size) throws IOException
- Throws:
IOException
-
copy
public static void copy(InputStream in, OutputStream os, long size) throws IOException
Copies from InputStream to OutputStream and does not close the streams intentionally.- Parameters:
in
- Stream to read from.os
- Stream to write to.size
- Buffersize for copying- Throws:
IOException
- Problem when copying
-
renameFile
public static boolean renameFile(File fromFile, File toFile)
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
public static void appendText(String fileName, String line) throws RuntimeException
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, StringBuilder buffer) throws IOException, FileNotFoundException
- Throws:
IOException
FileNotFoundException
-
readSmallFile
public static String readSmallFile(String fileName) throws IOException, FileNotFoundException
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
public static String readSmallFile(File file) throws IOException
- Throws:
IOException
-
getManagedFile
public static File getManagedFile(String file, File dir) throws IOException
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
public static void writeStringToFile(String s, File f) throws IOException
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
public static File[] findFilesInDir(File dir, String regexp)
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
public static String resourceToString(String resourceName)
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
public static byte[] resourceToBytes(String resourceName)
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
-
-