org.rhq.core.util.file
Class FileUtil

java.lang.Object
  extended by org.rhq.core.util.file.FileUtil

public class FileUtil
extends Object


Constructor Summary
FileUtil()
           
 
Method Summary
static boolean canWrite(File dir)
          Test if a directory is writable java.io.File#canWrite() has problems on windows for properly detecting if a directory is writable by the current user.
static void copyDirectory(File inDir, File outDir)
           
static void copyFile(File inFile, File outFile)
          Copy a file from one file to another
static void copyStream(InputStream is, OutputStream os)
          Deprecated. use StreamUtil for more methods like this - those are unit tested and used more
static void copyStream(InputStream is, OutputStream os, byte[] buf)
          Deprecated. use StreamUtil for more methods like this - those are unit tested and used more
static File createTempDirectory(String prefix, String suffix, File parentDirectory)
          Creates a temporary directory using the same algorithm as JDK's File.createTempFile.
static String escape(String name)
          Escape invalid characters in a filename, replacing with "_"
static String findString(String fname, String toFind)
           
static void forEachFile(File directory, FileVisitor visitor)
          Performs a breadth-first scan, calling visitor for each file in directory.
static Pattern generateRegex(List<PathFilter> filters)
          Takes a list of filters and compiles them into a regular expression that can be used for matching or filtering paths.
static List<File> getDirectoryFiles(File directory)
          Obtains the list of all files in the given directory and, recursively, all its subdirectories.
static String getFileName(String path, String separator)
          Return just the filename portion (the portion right of the last path separator string)
static String getRelativePath(File path, File base)
          The base attribute specifies what the directory base the relative path should be considered relative to.
static void purge(File dir, boolean deleteIt)
          Given a directory, this will recursively purge all child directories and files.
static String stripDriveLetter(StringBuilder path)
          Strips the drive letter from the given Windows path.
static String useForwardSlash(String path)
          Ensure that the path uses only forward slash.
static void writeFile(InputStream inputStream, File outputFile)
          Writes the content in the input stream to the specified file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

createTempDirectory

public static File createTempDirectory(String prefix,
                                       String suffix,
                                       File parentDirectory)
                                throws IOException
Creates a temporary directory using the same algorithm as JDK's File.createTempFile.

Throws:
IOException

purge

public static void purge(File dir,
                         boolean deleteIt)
Given a directory, this will recursively purge all child directories and files. If dir is actually a normal file, it will be deleted but only if deleteIt is true. If deleteIt is true, the directory itself will be deleted, otherwise it will remain (albeit empty).

Parameters:
dir - the directory to purge (if null, this method does nothing and returns normally)
deleteIt - if true delete the directory itself, otherwise leave it but purge its children

copyFile

public static void copyFile(File inFile,
                            File outFile)
                     throws FileNotFoundException,
                            IOException
Copy a file from one file to another

Throws:
FileNotFoundException
IOException

copyDirectory

public static void copyDirectory(File inDir,
                                 File outDir)
                          throws IOException
Throws:
IOException

getDirectoryFiles

public static List<File> getDirectoryFiles(File directory)
                                    throws IOException
Obtains the list of all files in the given directory and, recursively, all its subdirectories. Note that the returns list is only regular files - directory names are NOT in the list. Also, the names in the list are relative to the given directory.

Parameters:
directory - the directory whose files are to be returned
Returns:
list of files in the directory, not sorted in any particular order
Throws:
IOException - if directory does not exist or is not a directory

copyStream

public static void copyStream(InputStream is,
                              OutputStream os)
                       throws IOException
Deprecated. use StreamUtil for more methods like this - those are unit tested and used more

Copy a stream, using a buffer.

Throws:
IOException

copyStream

public static void copyStream(InputStream is,
                              OutputStream os,
                              byte[] buf)
                       throws IOException
Deprecated. use StreamUtil for more methods like this - those are unit tested and used more

Copy a stream, using a buffer.

Throws:
IOException

writeFile

public static void writeFile(InputStream inputStream,
                             File outputFile)
                      throws IOException
Writes the content in the input stream to the specified file. NOTE: inputStream will be closed by this.

Parameters:
inputStream - stream containing the content to write
outputFile - file to which the content will be written
Throws:
IOException - if any errors occur during the reading or writing

findString

public static String findString(String fname,
                                String toFind)
                         throws IOException
Throws:
IOException

getRelativePath

public static String getRelativePath(File path,
                                     File base)
The base attribute specifies what the directory base the relative path should be considered relative to. The base must be part of the absolute path specified by the path attribute.


escape

public static String escape(String name)
Escape invalid characters in a filename, replacing with "_"


canWrite

public static boolean canWrite(File dir)
                        throws IOException
Test if a directory is writable java.io.File#canWrite() has problems on windows for properly detecting if a directory is writable by the current user. For example, C:\Program Files is set to read-only, however the Administrator user is able to write to that directory

Throws:
IOException - If the File is not a directory

stripDriveLetter

public static String stripDriveLetter(StringBuilder path)
Strips the drive letter from the given Windows path. The drive letter is returned or null is returned if there was no drive letter in the path.

Parameters:
path - the path string that will be altered to have its drive letter stripped.
Returns:
if there was a drive letter, it will be returned. If no drive letter was in path, null is returned

useForwardSlash

public static String useForwardSlash(String path)
Ensure that the path uses only forward slash.

Parameters:
path -
Returns:
forward-slashed path, or null if path is null

getFileName

public static String getFileName(String path,
                                 String separator)
Return just the filename portion (the portion right of the last path separator string)

Parameters:
path -
separator -
Returns:
null if path is null, otherwise the trimmed filename

forEachFile

public static void forEachFile(File directory,
                               FileVisitor visitor)
Performs a breadth-first scan, calling visitor for each file in directory. Sub directories are scanned as well. Note that if visitor throws a RuntimeException it will not be called again as this method does not provide any exception handling.

Parameters:
directory - The directory over which to iterate
visitor - The callback to invoke for each file

generateRegex

public static Pattern generateRegex(List<PathFilter> filters)
Takes a list of filters and compiles them into a regular expression that can be used for matching or filtering paths. The pattern syntax supports regular expressions as well as the syntax used in Ant's file/path selectors. Here are some example patterns that can be specified in a filter:

Pattern Description
/etc/yum.conf exact match of the path
/etc/*.conf match any file /etc that has a .conf suffix
deploy/myapp-?.war Match any file in the deploy directory that starts with myapp- followed any one character and ending with a suffix of .war
jboss/server/**/*.war Matches all .war files under the server directory. Sub directories are included as well such that jboss/server/default/myapp.war jboss/server/production/myapp.war and jboss/server/default/myapp.ear/myapp.war all match

Parameters:
filters - Compiled into a regular expression
Returns:
A Pattern object that is a compilation of regular expressions built from the specified path filters


Copyright © 2008-2012 Red Hat, Inc.. All Rights Reserved.