public class FileUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
FileUtil.Pipe
A pipe when run reads from an input stream to an output stream, optionally sleeping between reads.
|
Modifier and Type | Field and Description |
---|---|
static java.io.FileFilter |
ALL
accept all files
|
static java.io.FileFilter |
aspectjSourceFileFilter |
static java.io.File |
DEFAULT_PARENT
default parent directory File when a file has a null parent
|
static java.io.FileFilter |
DIRS_AND_WRITABLE_CLASSES |
static java.util.List<java.lang.String> |
SOURCE_SUFFIXES
unmodifiable List of String source file suffixes (including leading ".")
|
static java.io.FileFilter |
ZIP_FILTER |
Modifier and Type | Method and Description |
---|---|
static boolean |
canReadDir(java.io.File dir) |
static boolean |
canReadFile(java.io.File file) |
static boolean |
canWriteDir(java.io.File dir) |
static boolean |
canWriteFile(java.io.File file) |
static int |
copyDir(java.io.File fromDir,
java.io.File toDir)
Copy contents of fromDir into toDir
|
static int |
copyDir(java.io.File fromDir,
java.io.File toDir,
java.lang.String fromSuffix,
java.lang.String toSuffix)
Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any.
|
static int |
copyDir(java.io.File fromDir,
java.io.File toDir,
java.lang.String fromSuffix,
java.lang.String toSuffix,
java.io.FileFilter delegate)
Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any.
|
static void |
copyFile(java.io.File fromFile,
java.io.File toFile)
Copy fromFile to toFile, handling file-file, dir-dir, and file-dir copies.
|
static java.io.File[] |
copyFiles(java.io.File srcDir,
java.lang.String[] relativePaths,
java.io.File destDir)
Copy files from source dir into destination directory, creating any needed directories.
|
static void |
copyStream(java.io.DataInputStream in,
java.io.PrintStream out)
do line-based copying
|
static void |
copyStream(java.io.InputStream in,
java.io.OutputStream out) |
static void |
copyStream(java.io.Reader in,
java.io.Writer out) |
static void |
copyValidFiles(java.io.File fromFile,
java.io.File toFile)
Copy file to file.
|
static int |
deleteContents(java.io.File dir)
Recursively delete the contents of dir, but not the dir itself
|
static int |
deleteContents(java.io.File dir,
java.io.FileFilter filter)
Recursively delete some contents of dir, but not the dir itself.
|
static int |
deleteContents(java.io.File dir,
java.io.FileFilter filter,
boolean deleteEmptyDirs)
Recursively delete some contents of dir, but not the dir itself.
|
static java.io.File |
ensureParentWritable(java.io.File path)
Ensure that the parent directory to path can be written.
|
static java.lang.String |
fileToClassName(java.io.File basedir,
java.io.File classFile)
Extract the name of a class from the path to its file.
|
static java.lang.String |
flatten(java.io.File[] files,
java.lang.String infix)
Render a set of files to String as a path by getting absolute paths of each and delimiting with infix.
|
static java.lang.String |
flatten(java.lang.String[] paths,
java.lang.String infix)
Flatten File[] to String.
|
static java.lang.String[] |
getAbsolutePaths(java.io.File[] files) |
static java.io.File[] |
getBaseDirFiles(java.io.File basedir,
java.lang.String[] paths)
Convert String[] paths to File[] as offset of base directory
|
static java.io.File[] |
getBaseDirFiles(java.io.File basedir,
java.lang.String[] paths,
java.lang.String[] suffixes)
Convert String[] paths to File[] as offset of base directory
|
static java.io.File |
getBestFile(java.io.File file)
Render as best file, canonical or absolute.
|
static java.io.File |
getBestFile(java.lang.String[] paths)
Get best File for the first-readable path in input paths, treating entries prefixed "sp:" as system property keys.
|
static java.lang.String |
getBestPath(java.io.File file)
Render as best path, canonical or absolute.
|
static java.net.URL |
getFileURL(java.io.File file)
Get URL for a File.
|
static java.net.URL[] |
getFileURLs(java.io.File[] files) |
static java.lang.String[] |
getPaths(java.io.File[] files) |
static java.lang.String[] |
getPaths(java.util.List<java.io.File> files) |
static java.io.InputStream |
getStreamFromZip(java.lang.String zipFile,
java.lang.String name) |
static java.io.File |
getTempDir(java.lang.String name)
Make a new temporary directory in the same directory that the system uses for temporary files, or if that files, in the
current directory.
|
static boolean |
hasSourceSuffix(java.io.File file) |
static boolean |
hasSourceSuffix(java.lang.String path) |
static boolean |
isZipFile(java.io.File file) |
static java.util.List<java.lang.String> |
lineSeek(java.lang.String sought,
java.util.List<java.lang.String> sources,
boolean listAll,
java.io.PrintStream errorSink)
Do line-based search for literal text in source files, returning file:line where found.
|
static java.lang.String |
lineSeek(java.lang.String sought,
java.lang.String sourcePath,
boolean listAll,
java.util.ArrayList<java.lang.String> sink)
Do line-based search for literal text in source file, returning line where found as a String in the form
{sourcePath}:line:column submitted to the collecting parameter sink.
|
static java.util.List<java.io.File> |
listClassFiles(java.io.File dir)
Recursively list .class files in specified directory
|
static java.lang.String[] |
listFiles(java.io.File srcDir)
Recursively list files in srcDir.
|
static java.io.File[] |
listFiles(java.io.File srcDir,
java.io.FileFilter fileFilter)
Recursively list files in srcDir.
|
static java.util.List<java.lang.String> |
makeClasspath(java.net.URL[] urls) |
static java.io.File |
makeNewChildDir(java.io.File parent,
java.lang.String child)
Make a new child directory of parent
|
static java.io.BufferedOutputStream |
makeOutputStream(java.io.File file) |
static java.lang.String |
normalizedPath(java.io.File file)
Normalize path for comparisons by rendering absolute trimming and changing '\\' to '/'
|
static java.lang.String |
normalizedPath(java.io.File file,
java.io.File basedir)
Normalize path for comparisons by rendering absolute, clipping basedir prefix, trimming and changing '\\' to '/'
|
static byte[] |
readAsByteArray(java.io.File file)
Returns the contents of this file as a byte[]
|
static byte[] |
readAsByteArray(java.io.InputStream inStream)
Reads this input stream and returns contents as a byte[]
|
static java.lang.String |
readAsString(java.io.File file)
Returns the contents of this file as a String
|
static boolean[] |
readBooleanArray(java.io.DataInputStream s)
Reads a boolean array with our encoding
|
static int[] |
readIntArray(java.io.DataInputStream s)
Reads an int array with our encoding
|
static java.lang.String[] |
readStringArray(java.io.DataInputStream s)
Reads an int array with our encoding
|
static boolean |
sleepPastFinalModifiedTime(java.io.File[] files)
Sleep until after the last last-modified stamp from the files.
|
static int |
sourceSuffixLength(java.io.File file) |
static int |
sourceSuffixLength(java.lang.String path) |
static void |
throwIaxUnlessCanReadDir(java.io.File dir,
java.lang.String label) |
static void |
throwIaxUnlessCanWriteDir(java.io.File dir,
java.lang.String label) |
static void |
throwIaxUnlessCanWriteFile(java.io.File file,
java.lang.String label) |
static java.lang.String |
weakNormalize(java.lang.String path)
Weakly normalize path for comparisons by trimming and changing '\\' to '/'
|
static java.lang.String |
writeAsString(java.io.File file,
java.lang.String contents)
Write contents to file, returning null on success or error message otherwise.
|
static void |
writeBooleanArray(boolean[] a,
java.io.DataOutputStream s)
Writes a boolean array with our encoding
|
static void |
writeIntArray(int[] a,
java.io.DataOutputStream s)
Writes an int array with our encoding
|
static void |
writeStringArray(java.lang.String[] a,
java.io.DataOutputStream s)
Writes an int array with our encoding
|
static int |
zipSuffixLength(java.io.File file) |
static int |
zipSuffixLength(java.lang.String path) |
public static final java.io.File DEFAULT_PARENT
public static final java.util.List<java.lang.String> SOURCE_SUFFIXES
public static final java.io.FileFilter ZIP_FILTER
public static final java.io.FileFilter ALL
public static final java.io.FileFilter DIRS_AND_WRITABLE_CLASSES
public static final java.io.FileFilter aspectjSourceFileFilter
public static boolean isZipFile(java.io.File file)
public static int zipSuffixLength(java.io.File file)
public static int zipSuffixLength(java.lang.String path)
public static boolean hasSourceSuffix(java.io.File file)
public static boolean hasSourceSuffix(java.lang.String path)
public static int sourceSuffixLength(java.io.File file)
public static int sourceSuffixLength(java.lang.String path)
public static boolean canReadDir(java.io.File dir)
public static boolean canReadFile(java.io.File file)
public static boolean canWriteDir(java.io.File dir)
public static boolean canWriteFile(java.io.File file)
public static void throwIaxUnlessCanReadDir(java.io.File dir, java.lang.String label)
java.lang.IllegalArgumentException
- unless dir is a readable directorypublic static void throwIaxUnlessCanWriteFile(java.io.File file, java.lang.String label)
java.lang.IllegalArgumentException
- unless file is readable and not a directorypublic static void throwIaxUnlessCanWriteDir(java.io.File dir, java.lang.String label)
java.lang.IllegalArgumentException
- unless dir is a readable directorypublic static java.lang.String[] getPaths(java.io.File[] files)
public static java.lang.String[] getPaths(java.util.List<java.io.File> files)
public static java.lang.String fileToClassName(java.io.File basedir, java.io.File classFile)
basedir
- the File of the base directory (prefix of classFile)classFile
- the File of the class to extract the name forjava.lang.IllegalArgumentException
- if classFile is null or does not end with ".class" or a non-null basedir is not a prefix of
classFilepublic static java.lang.String normalizedPath(java.io.File file, java.io.File basedir)
file
- the File with the path to normalizebasedir
- the File for the prefix of the file to normalize - ignored if nulljava.lang.IllegalArgumentException
- if basedir is not a prefix of filepublic static java.lang.String flatten(java.io.File[] files, java.lang.String infix)
files
- the File[] to flatten - may be null or emptyinfix
- the String delimiter internally between entries (if null, then use File.pathSeparator). (alias to
flatten(getAbsolutePaths(files), infix)
public static java.lang.String flatten(java.lang.String[] paths, java.lang.String infix)
files
- the File[] of paths to flatten - null ignoredinfix
- the String infix to use - null treated as File.pathSeparatorpublic static java.lang.String normalizedPath(java.io.File file)
public static java.lang.String weakNormalize(java.lang.String path)
public static java.io.File getBestFile(java.lang.String[] paths)
paths
- the String[] of paths to check.public static java.io.File getBestFile(java.io.File file)
file
- the File to get the best File for (not null)java.lang.IllegalArgumentException
- if file is nullpublic static java.lang.String getBestPath(java.io.File file)
file
- the File to get the path for (not null)java.lang.IllegalArgumentException
- if file is nullpublic static java.lang.String[] getAbsolutePaths(java.io.File[] files)
public static int deleteContents(java.io.File dir)
public static int deleteContents(java.io.File dir, java.io.FileFilter filter)
public static int deleteContents(java.io.File dir, java.io.FileFilter filter, boolean deleteEmptyDirs)
dir
- the File directory (if a file, the the file is deleted)public static int copyDir(java.io.File fromDir, java.io.File toDir) throws java.io.IOException
fromDir
- must exist and be readabletoDir
- must exist or be creatable and be writablejava.io.IOException
public static int copyDir(java.io.File fromDir, java.io.File toDir, java.lang.String fromSuffix, java.lang.String toSuffix) throws java.io.IOException
fromSuffix
- select files with this suffix - select all if null or emptytoSuffix
- replace fromSuffix with toSuffix in the destination file name - ignored if null or empty, appended to name if
fromSuffix is null or emptyjava.io.IOException
public static int copyDir(java.io.File fromDir, java.io.File toDir, java.lang.String fromSuffix, java.lang.String toSuffix, java.io.FileFilter delegate) throws java.io.IOException
fromSuffix
- select files with this suffix - select all if null or emptytoSuffix
- replace fromSuffix with toSuffix in the destination file name - ignored if null or empty, appended to name if
fromSuffix is null or emptyjava.io.IOException
public static java.lang.String[] listFiles(java.io.File srcDir)
public static java.io.File[] listFiles(java.io.File srcDir, java.io.FileFilter fileFilter)
public static java.util.List<java.io.File> listClassFiles(java.io.File dir)
public static java.io.File[] getBaseDirFiles(java.io.File basedir, java.lang.String[] paths)
basedir
- the non-null File base directory for File to create with pathspaths
- the String[] of paths to createpublic static java.io.File[] getBaseDirFiles(java.io.File basedir, java.lang.String[] paths, java.lang.String[] suffixes)
basedir
- the non-null File base directory for File to create with pathspaths
- the String[] of paths to createsuffixes
- the String[] of suffixes to limit sources to - ignored if nullpublic static java.io.File[] copyFiles(java.io.File srcDir, java.lang.String[] relativePaths, java.io.File destDir) throws java.lang.IllegalArgumentException, java.io.IOException
srcDir
- an existing, readable directory containing relativePaths filesrelativePaths
- a set of paths relative to srcDir to readable File to copydestDir
- an existing, writable directory to copy files tojava.lang.IllegalArgumentException
- if input invalid, IOException if operations failjava.io.IOException
public static void copyFile(java.io.File fromFile, java.io.File toFile) throws java.io.IOException
fromFile
- the File path of the file or directory to copy - must be readabletoFile
- the File path of the target file or directory - must be writable (will be created if it does not exist)java.io.IOException
public static java.io.File ensureParentWritable(java.io.File path)
path
- the File path whose parent should be writablejava.lang.IllegalArgumentException
- if parent cannot be written or path is null.public static void copyValidFiles(java.io.File fromFile, java.io.File toFile) throws java.io.IOException
fromFile
- the File to copy (readable, non-null file)toFile
- the File to copy to (non-null, parent dir exists)java.io.IOException
public static void copyStream(java.io.DataInputStream in, java.io.PrintStream out) throws java.io.IOException
java.io.IOException
public static void copyStream(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
java.io.IOException
public static void copyStream(java.io.Reader in, java.io.Writer out) throws java.io.IOException
java.io.IOException
public static java.io.File makeNewChildDir(java.io.File parent, java.lang.String child)
parent
- a File for the parent (writable)child
- a prefix for the child directorypublic static java.io.File getTempDir(java.lang.String name)
name
- the preferred (simple) name of the directory - may be null.public static java.net.URL[] getFileURLs(java.io.File[] files)
public static java.net.URL getFileURL(java.io.File file)
file
- the File to convert to URL (not null)public static java.lang.String writeAsString(java.io.File file, java.lang.String contents)
file
- the File to write (not null)contents
- the String to write (use "" if null)public static boolean[] readBooleanArray(java.io.DataInputStream s) throws java.io.IOException
java.io.IOException
public static void writeBooleanArray(boolean[] a, java.io.DataOutputStream s) throws java.io.IOException
java.io.IOException
public static int[] readIntArray(java.io.DataInputStream s) throws java.io.IOException
java.io.IOException
public static void writeIntArray(int[] a, java.io.DataOutputStream s) throws java.io.IOException
java.io.IOException
public static java.lang.String[] readStringArray(java.io.DataInputStream s) throws java.io.IOException
java.io.IOException
public static void writeStringArray(java.lang.String[] a, java.io.DataOutputStream s) throws java.io.IOException
java.io.IOException
public static java.lang.String readAsString(java.io.File file) throws java.io.IOException
java.io.IOException
public static byte[] readAsByteArray(java.io.File file) throws java.io.IOException
java.io.IOException
public static byte[] readAsByteArray(java.io.InputStream inStream) throws java.io.IOException
java.io.IOException
public static java.io.InputStream getStreamFromZip(java.lang.String zipFile, java.lang.String name)
public static java.util.List<java.lang.String> lineSeek(java.lang.String sought, java.util.List<java.lang.String> sources, boolean listAll, java.io.PrintStream errorSink)
sought
- the String text to seek in the filesources
- the List of String paths to the source fileslistAll
- if false, only list first match in fileerrorSink
- the PrintStream to print any errors to (one per line) (use null to silently ignore errors)public static java.lang.String lineSeek(java.lang.String sought, java.lang.String sourcePath, boolean listAll, java.util.ArrayList<java.lang.String> sink)
sought
- the String text to seek in the filesources
- the List of String paths to the source fileslistAll
- if false, only list first match in fileList
- sink the List for String entries of the form {sourcePath}:line:columnpublic static java.io.BufferedOutputStream makeOutputStream(java.io.File file) throws java.io.FileNotFoundException
java.io.FileNotFoundException
public static boolean sleepPastFinalModifiedTime(java.io.File[] files)
files
- the File[] of files to inspect for last modified times (this ignores null or empty files array and null or
non-existing components of files array)public static java.util.List<java.lang.String> makeClasspath(java.net.URL[] urls)