basename
public static String basename(String filename)
Returns the filename portion of a file specification string.
Matches the equally named unix command.
- The filename string without extension.
basename
public static String basename(String filename,
String suffix)
Returns the filename portion of a file specification string.
Matches the equally named unix command.
byteCountToDisplaySize
public static String byteCountToDisplaySize(int size)
Returns a human-readable version of the file size (original is in
bytes).
size
- The number of bytes.
- A human-readable display value (includes units).
catPath
public static String catPath(String lookupPath,
String path)
Will concatenate 2 paths. Paths with
..
will be
properly handled.
Eg.,
/a/b/c
+
d
=
/a/b/d
/a/b/c
+
../d
=
/a/d
Thieved from Tomcat sources...
- The concatenated paths, or null if error occurs
cleanDirectory
public static void cleanDirectory(File directory)
throws IOException
Clean a directory without deleting it.
cleanDirectory
public static void cleanDirectory(String directory)
throws IOException
Clean a directory without deleting it.
contentEquals
public static boolean contentEquals(File file1,
File file2)
throws IOException
Compare the contents of two files to determine if they are equal or not.
file1
- the first filefile2
- the second file
- true if the content of the files are equal or they both don't exist, false otherwise
copyDirectory
public static void copyDirectory(File sourceDirectory,
File destinationDirectory)
throws IOException
copyDirectory
public static void copyDirectory(File sourceDirectory,
File destinationDirectory,
String includes,
String excludes)
throws IOException
copyDirectoryStructure
public static void copyDirectoryStructure(File sourceDirectory,
File destinationDirectory)
throws IOException
Copies a entire directory structure.
Note:
- It will include empty directories.
- The
sourceDirectory
must exists.
sourceDirectory
- destinationDirectory
-
copyDirectoryStructureIfModified
public static void copyDirectoryStructureIfModified(File sourceDirectory,
File destinationDirectory)
throws IOException
Copies an entire directory structure but only source files with timestamp later than the destinations'.
Note:
- It will include empty directories.
- The
sourceDirectory
must exists.
sourceDirectory
- destinationDirectory
-
copyFile
public static void copyFile(File source,
File destination)
throws IOException
Copy file from source to destination. The directories up to destination
will be
created if they don't already exist. destination
will be overwritten if it
already exists.
source
- An existing non-directory File
to copy bytes from.destination
- A non-directory File
to write bytes to (possibly
overwriting).
copyFile
public static void copyFile(File from,
File to,
String encoding,
FileUtils.FilterWrapper[] wrappers)
throws IOException
copyFileIfModified
public static boolean copyFileIfModified(File source,
File destination)
throws IOException
Copy file from source to destination only if source timestamp is later than the destination timestamp.
The directories up to destination
will be created if they don't already exist.
destination
will be overwritten if it already exists.
source
- An existing non-directory File
to copy bytes from.destination
- A non-directory File
to write bytes to (possibly
overwriting).
copyFileToDirectory
public static void copyFileToDirectory(File source,
File destinationDirectory)
throws IOException
Copy file from source to destination. If destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
source
- An existing File
to copy.destinationDirectory
- A directory to copy source
into.
copyFileToDirectory
public static void copyFileToDirectory(String source,
String destinationDirectory)
throws IOException
Copy file from source to destination. If destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
source
- An existing File
to copy.destinationDirectory
- A directory to copy source
into.
copyFileToDirectoryIfModified
public static void copyFileToDirectoryIfModified(File source,
File destinationDirectory)
throws IOException
Copy file from source to destination only if source is newer than the target file.
If destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
source
- An existing File
to copy.destinationDirectory
- A directory to copy source
into.
copyFileToDirectoryIfModified
public static void copyFileToDirectoryIfModified(String source,
String destinationDirectory)
throws IOException
Copy file from source to destination only if source is newer than the target file.
If destinationDirectory
does not exist, it
(and any parent directories) will be created. If a file source
in
destinationDirectory
exists, it will be overwritten.
source
- An existing File
to copy.destinationDirectory
- A directory to copy source
into.
copyURLToFile
public static void copyURLToFile(URL source,
File destination)
throws IOException
Copies bytes from the URL source
to a file destination
.
The directories up to destination
will be created if they don't already exist.
destination
will be overwritten if it already exists.
source
- A URL
to copy bytes from.destination
- A non-directory File
to write bytes to (possibly
overwriting).
createTempFile
public static File createTempFile(String prefix,
String suffix,
File parentDir)
Create a temporary file in a given directory.
The file denoted by the returned abstract pathname did not
exist before this method was invoked, any subsequent invocation
of this method will yield a different file name.
The filename is prefixNNNNNsuffix where NNNN is a random number
This method is different to File.createTempFile of JDK 1.2
as it doesn't create the file itself.
It uses the location pointed to by java.io.tmpdir
when the parentDir attribute is
null.
prefix
- prefix before the random numbersuffix
- file extension; include the '.'parentDir
- Directory to create the temporary file in -
java.io.tmpdir used if not specificed
- a File reference to the new temporary file.
deleteDirectory
public static void deleteDirectory(File directory)
throws IOException
Recursively delete a directory.
deleteDirectory
public static void deleteDirectory(String directory)
throws IOException
Recursively delete a directory.
dirname
public static String dirname(String filename)
Returns the directory path portion of a file specification string.
Matches the equally named unix command.
- The directory portion excluding the ending file separator.
extension
public static String extension(String filename)
Returns the extension portion of a file specification string.
This everything after the last dot '.' in the filename (NOT including
the dot).
fileAppend
public static void fileAppend(String fileName,
String data)
throws IOException
Appends data to a file. The file will be created if it does not exist.
fileName
- The name of the file to write.data
- The content to write to the file.
fileDelete
public static void fileDelete(String fileName)
Deletes a file.
fileName
- The name of the file to delete.
fileExists
public static boolean fileExists(String fileName)
Check if a file exits.
fileName
- The name of the file to check.
fileRead
public static String fileRead(File file)
throws IOException
fileRead
public static String fileRead(String file)
throws IOException
fileWrite
public static void fileWrite(String fileName,
String data)
throws IOException
Writes data to a file. The file will be created if it does not exist.
fileName
- The name of the file to write.data
- The content to write to the file.
filename
public static String filename(String filename)
Returns the filename portion of a file specification string.
- The filename string with extension.
forceDelete
public static void forceDelete(File file)
throws IOException
Delete a file. If file is directory delete it and all sub-directories.
forceDelete
public static void forceDelete(String file)
throws IOException
Delete a file. If file is directory delete it and all sub-directories.
forceDeleteOnExit
public static void forceDeleteOnExit(File file)
throws IOException
Schedule a file to be deleted when JVM exits.
If file is directory delete it and all sub-directories.
forceMkdir
public static void forceMkdir(File file)
throws IOException
Make a directory. If there already exists a file with specified name or
the directory is unable to be created then an exception is thrown.
getDefaultExcludes
public static String[] getDefaultExcludes()
getDefaultExcludesAsList
public static List getDefaultExcludesAsList()
getDirectoryNames
public static List getDirectoryNames(File directory,
String includes,
String excludes,
boolean includeBasedir)
throws IOException
Return a list of directories as String depending options.
This method use case sensitive file name.
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of file
- a list of directories as String
getDirectoryNames
public static List getDirectoryNames(File directory,
String includes,
String excludes,
boolean includeBasedir,
boolean isCaseSensitive)
throws IOException
Return a list of directories as String depending options.
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of fileisCaseSensitive
- true if case sensitive
- a list of directories as String
getExtension
public static String getExtension(String filename)
Get extension from filename.
ie
foo.txt --> "txt"
a\b\c.jpg --> "jpg"
a\b\c --> ""
- the extension of filename or "" if none
getFile
public static File getFile(String fileName)
Creates a file handle.
fileName
- The name of the file.
getFileAndDirectoryNames
public static List getFileAndDirectoryNames(File directory,
String includes,
String excludes,
boolean includeBasedir,
boolean isCaseSensitive,
boolean getFiles,
boolean getDirectories)
throws IOException
Return a list of files as String depending options.
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of fileisCaseSensitive
- true if case sensitivegetFiles
- true if get filesgetDirectories
- true if get directories
- a list of files as String
getFileNames
public static List getFileNames(File directory,
String includes,
String excludes,
boolean includeBasedir)
throws IOException
Return a list of files as String depending options.
This method use case sensitive file name.
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of file
- a list of files as String
getFileNames
public static List getFileNames(File directory,
String includes,
String excludes,
boolean includeBasedir,
boolean isCaseSensitive)
throws IOException
Return a list of files as String depending options.
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each String of fileisCaseSensitive
- true if case sensitive
- a list of files as String
getFiles
public static List getFiles(File directory,
String includes,
String excludes)
throws IOException
Return the files contained in the directory, using inclusion and exclusion Ant patterns,
including the directory name in each of the files
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separated
getFileNames( File, String, String, boolean )
getFiles
public static List getFiles(File directory,
String includes,
String excludes,
boolean includeBasedir)
throws IOException
Return the files contained in the directory, using inclusion and exclusion Ant patterns
directory
- the directory to scanincludes
- the includes pattern, comma separatedexcludes
- the excludes pattern, comma separatedincludeBasedir
- true to include the base dir in each file
getFileNames( File, String, String, boolean )
getFilesFromExtension
public static String[] getFilesFromExtension(String directory,
String[] extensions)
Given a directory and an array of extensions return an array of compliant files.
TODO Should an ignore list be passed in?
TODO Should a recurse flag be passed in?
The given extensions should be like "java" and not like ".java"
getPath
public static String getPath(String filepath)
Get path from filename. Roughly equivalent to the unix command
dirname
.
ie.
a/b/c.txt --> a/b
a.txt --> ""
getPath
public static String getPath(String filepath,
char fileSeparatorChar)
Get path from filename.
ie.
a/b/c.txt --> a/b
a.txt --> ""
loadFile
public static List loadFile(File file)
throws IOException
mkdir
public static void mkdir(String dir)
Simple way to make a directory
normalize
public static String normalize(String path)
Normalize a path.
Eliminates "/../" and "/./" in a string. Returns
null
if the ..'s went past the
root.
Eg:
/foo// --> /foo/
/foo/./ --> /foo/
/foo/../bar --> /bar
/foo/../bar/ --> /bar/
/foo/../bar/../baz --> /baz
//foo//./bar --> /foo/bar
/../ --> null
path
- the path to normalize
- the normalized String, or
null
if too many ..'s.
removeExtension
public static String removeExtension(String filename)
Remove extension from filename.
ie
foo.txt --> foo
a\b\c.jpg --> a\b\c
a\b\c --> a\b\c
- the filename minus extension
removePath
public static String removePath(String filepath)
Remove path from filename. Equivalent to the unix command
basename
ie.
a/b/c.txt --> c.txt
a.txt --> a.txt
removePath
public static String removePath(String filepath,
char fileSeparatorChar)
Remove path from filename.
ie.
a/b/c.txt --> c.txt
a.txt --> a.txt
rename
public static void rename(File from,
File to)
throws IOException
Renames a file, even if that involves crossing file system boundaries.
This will remove
to
(if it exists), ensure that
to
's parent directory exists and move
from
, which involves deleting
from
as
well.
from
- the file to moveto
- the new file name
resolveFile
public static File resolveFile(File baseFile,
String filename)
Resolve a file filename
to it's canonical form. If filename
is
relative (doesn't start with /
), it will be resolved relative to
baseFile
, otherwise it is treated as a normal root-relative path.
baseFile
- Where to resolve filename
from, if filename
is
relative.filename
- Absolute or relative file path to resolve.
- The canonical
File
of filename
.
sizeOfDirectory
public static long sizeOfDirectory(File directory)
Recursively count size of a directory.
- size of directory in bytes.
sizeOfDirectory
public static long sizeOfDirectory(String directory)
Recursively count size of a directory.
- size of directory in bytes.
toFile
public static File toFile(URL url)
Convert from a URL
to a File
.
- The equivalent
File
object, or null
if the URL's protocol
is not file
toURLs
public static URL[] toURLs(File[] files)
throws IOException
Convert the array of Files into a list of URLs.
files
- the array of files
waitFor
public static boolean waitFor(File file,
int seconds)
waitFor
public static boolean waitFor(String fileName,
int seconds)
Waits for NFS to propagate a file creation, imposing a timeout.
fileName
- The name of the file.seconds
- The maximum time in seconds to wait.