public class FileUtil extends Object
| Constructor and Description |
|---|
FileUtil() |
| Modifier and Type | Method and Description |
|---|---|
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 |
compressFile(File originalFile)
Compressed the data found in the file.
|
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 void |
decompressFile(File originalFile)
Decompresses the compressed data found in the file that was compressed with
compressFile(File). |
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 boolean |
isAbsolutePath(String path)
Under certain conditions, it might be desired to consider a path that is technically a Windows relative path
to be absolute.
|
static Boolean |
isNewer(File file1,
File file2)
This will check to see if file1 is newer than file2.
|
static File |
normalizePath(File file)
Normalizes the path of the file by removing any ".." and "."
This method behaves very similar to Java7's
Path.normalize() method with the exception of dealing with
paths jumping "above" the FS root. |
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 |
useBackwardSlash(String path)
Ensure that the path uses only forward slash.
|
static String |
useForwardSlash(String path)
Ensure that the path uses only forward slash.
|
static String |
useNativeSlash(String path)
Ensure that the path uses only the proper file separator for the OS.
|
static void |
writeFile(InputStream inputStream,
File outputFile)
Writes the content in the input stream to the specified file.
|
public static Boolean isNewer(File file1, File file2)
true is returned.
false is returned if file1's date is the same or older than file2's date.
null is returned if any of these conditions are true:
file1 - file2 - public static File createTempDirectory(String prefix, String suffix, File parentDirectory) throws IOException
IOExceptionpublic static void purge(File dir, boolean deleteIt)
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 childrenpublic static void copyFile(File inFile, File outFile) throws FileNotFoundException, IOException
FileNotFoundExceptionIOExceptionpublic static void copyDirectory(File inDir, File outDir) throws IOException
IOExceptionpublic static List<File> getDirectoryFiles(File directory) throws IOException
directory - the directory whose files are to be returnedIOException - if directory does not exist or is not a directory@Deprecated public static void copyStream(InputStream is, OutputStream os) throws IOException
StreamUtil for more methods like this - those are unit tested and used moreIOException@Deprecated public static void copyStream(InputStream is, OutputStream os, byte[] buf) throws IOException
StreamUtil for more methods like this - those are unit tested and used moreIOExceptionpublic static void writeFile(InputStream inputStream, File outputFile) throws IOException
inputStream - stream containing the content to writeoutputFile - file to which the content will be writtenIOException - if any errors occur during the reading or writingpublic static String findString(String fname, String toFind) throws IOException
IOExceptionpublic static String getRelativePath(File path, File base)
public static String escape(String name)
public static boolean canWrite(File dir) throws IOException
IOException - If the File is not a directorypublic static String stripDriveLetter(StringBuilder path)
null is returned if there was no drive letter in the path.path - the path string that will be altered to have its drive letter stripped.public static String useForwardSlash(String path)
path - public static String useBackwardSlash(String path)
path - public static String useNativeSlash(String path)
path - public static String getFileName(String path, String separator)
path - separator - public static void forEachFile(File directory, FileVisitor visitor)
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.directory - The directory over which to iteratevisitor - The callback to invoke for each filepublic static Pattern generateRegex(List<PathFilter> filters)
| 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 |
filters - Compiled into a regular expressionpublic static File normalizePath(File file)
Path.normalize() method with the exception of dealing with
paths jumping "above" the FS root.
Java7's normalization will normalize a path like C:\..\asdf to C:\asdf, while this method will
return null, because it understands C:\..\asdf as an attempt to "go above" the file system root.public static boolean isAbsolutePath(String path)
path - the path to see if it really can be considered absolutepublic static void compressFile(File originalFile) throws IOException
decompressFile(File).originalFile - IOExceptionpublic static void decompressFile(File originalFile) throws IOException
compressFile(File).
If the file was not previously compressed, an exception is thrown.originalFile - IOExceptionCopyright © 2008-2014 Red Hat, Inc.. All Rights Reserved.