com.sun.enterprise.util.io
Class FileUtils

java.lang.Object
  extended by com.sun.enterprise.util.io.FileUtils

public class FileUtils
extends Object


Constructor Summary
FileUtils()
           
 
Method Summary
static void appendText(String fileName, String line)
          Appends the given line at the end of given text file.
static void appendText(String fileName, StringBuffer 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)
           
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 specified
static 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 boolean deleteFile(File f)
          Delete a file.
static File[] findFilesInDir(File dir, String regexp)
          Find files matching the regular expression in the given directory
static String formatFileCollection(Collection<File> files)
          Creates a String listing the absolute paths of files, separated by the platform's line separator.
static Set getAllFilesAndDirectoriesUnder(File directory)
           
static Set 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 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 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 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 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 searchDir(File dirName, String ext)
          Returns a list of abstract pathnames that matches with the given file extension.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

public FileUtils()
Method Detail

listFiles

public static File[] listFiles(File f)

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)

safeGetCanonicalPath

public static String safeGetCanonicalPath(File f)

safeGetCanonicalFile

public static File safeGetCanonicalFile(File f)

hasExtension

public static boolean hasExtension(String filename,
                                   String ext)

hasExtension

public static boolean hasExtension(File f,
                                   String ext)

hasExtensionIgnoreCase

public static boolean hasExtensionIgnoreCase(String filename,
                                             String ext)

hasExtensionIgnoreCase

public static boolean hasExtensionIgnoreCase(File f,
                                             String ext)

isLegalFilename

public static boolean isLegalFilename(String filename)

isFriendlyFilename

public static boolean isFriendlyFilename(String filename)

makeLegalFilename

public static String makeLegalFilename(String filename)

makeLegalNoBlankFileName

public static String makeLegalNoBlankFileName(String filename)

makeFriendlyFilename

public static String makeFriendlyFilename(String filename)

makeFriendlyFilenameNoExtension

public static String makeFriendlyFilenameNoExtension(String filename)

makeFriendlyFilenameExtension

public static String makeFriendlyFilenameExtension(String filename)

revertFriendlyFilenameExtension

public static String revertFriendlyFilenameExtension(String filename)

revertFriendlyFilename

public static String revertFriendlyFilename(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

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 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 getAllFilesUnder(File directory,
                                   FilenameFilter filenameFilter,
                                   boolean relativize)
                            throws IOException
Throws:
IOException

getAllFilesAndDirectoriesUnder

public static Set 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

getDirectory

public static File getDirectory(File f)

createTempFile

public static File createTempFile(File directory)

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 begin
ext - file extension to look for
Returns:
an array of abstract pathnames that matches with the extension

searchDir

public static List 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 begin
ext - 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 copy
to - 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 copy
fout - 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 copy
dout - 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 file
fout - 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
Throws:
IOException

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 renamed
toFile - 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 to
line - 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:
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 is small .

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 for
dir - - 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 file
f - 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 search
regexp - the regular expression pattern
Returns:
either an array of matching File objects or an empty array. Guaranteed to never return null


Copyright © 2012 GlassFish Community. All Rights Reserved.