Package org.aspectj.util
Klasse FileUtil
java.lang.Object
org.aspectj.util.FileUtil
- Autor:
- Andy Clement, Kris De Volder
-
Verschachtelte Klassen - Übersicht
Modifizierer und TypKlasseBeschreibungstatic class
A pipe when run reads from an input stream to an output stream, optionally sleeping between reads. -
Feldübersicht
Modifizierer und TypFeldBeschreibungstatic final FileFilter
accept all filesstatic final FileFilter
static final File
default parent directory File when a file has a null parentstatic final FileFilter
unmodifiable List of String source file suffixes (including leading ".")static final FileFilter
-
Methodenübersicht
Modifizierer und TypMethodeBeschreibungstatic boolean
canReadDir
(File dir) static boolean
canReadFile
(File file) static boolean
canWriteDir
(File dir) static boolean
canWriteFile
(File file) static int
Copy contents of fromDir into toDirstatic int
Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any.static int
Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any.static void
Copy fromFile to toFile, handling file-file, dir-dir, and file-dir copies.static File[]
Copy files from source dir into destination directory, creating any needed directories.static void
copyStream
(DataInputStream in, PrintStream out) do line-based copyingstatic void
copyStream
(InputStream in, OutputStream out) static void
copyStream
(Reader in, Writer out) static void
copyValidFiles
(File fromFile, File toFile) Copy file to file.static int
deleteContents
(File dir) Recursively delete the contents of dir, but not the dir itselfstatic int
deleteContents
(File dir, FileFilter filter) Recursively delete some contents of dir, but not the dir itself.static int
deleteContents
(File dir, FileFilter filter, boolean deleteEmptyDirs) Recursively delete some contents of dir, but not the dir itself.static File
ensureParentWritable
(File path) Ensure that the parent directory to path can be written.static String
fileToClassName
(File basedir, File classFile) Extract the name of a class from the path to its file.static String
Render a set of files to String as a path by getting absolute paths of each and delimiting with infix.static String
Flatten File[] to String.static String[]
getAbsolutePaths
(File[] files) static File[]
getBaseDirFiles
(File basedir, String[] paths) Convert String[] paths to File[] as offset of base directorystatic File[]
getBaseDirFiles
(File basedir, String[] paths, String[] suffixes) Convert String[] paths to File[] as offset of base directorystatic File
getBestFile
(File file) Render as best file, canonical or absolute.static File
getBestFile
(String[] paths) Get best File for the first-readable path in input paths, treating entries prefixed "sp:" as system property keys.static File
getBestFile
(String[] paths, boolean mustBeJar) static String
getBestPath
(File file) Render as best path, canonical or absolute.static URL
getFileURL
(File file) Get URL for a File.static URL[]
getFileURLs
(File[] files) static String[]
static String[]
static InputStream
getStreamFromZip
(String zipFile, String name) static File
getTempDir
(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
(File file) static boolean
hasSourceSuffix
(String path) static boolean
static String
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.lineSeek
(String sought, List<String> sources, boolean listAll, PrintStream errorSink) Do line-based search for literal text in source files, returning file:line where found.listClassFiles
(File dir) Recursively list .class files in specified directorystatic String[]
Recursively list files in srcDir.static File[]
listFiles
(File srcDir, FileFilter fileFilter) Recursively list files in srcDir.makeClasspath
(URL[] urls) static File
makeNewChildDir
(File parent, String child) Make a new child directory of parentstatic BufferedOutputStream
makeOutputStream
(File file) static String
normalizedPath
(File file) Normalize path for comparisons by rendering absolute trimming and changing '\\' to '/'static String
normalizedPath
(File file, File basedir) Normalize path for comparisons by rendering absolute, clipping basedir prefix, trimming and changing '\\' to '/'static byte[]
readAsByteArray
(File file) Returns the contents of this file as a byte[]static byte[]
readAsByteArray
(InputStream inStream) Reads this input stream and returns contents as a byte[]readAsLines
(File file) static String
readAsString
(File file) Returns the contents of this file as a Stringstatic boolean[]
Reads a boolean array with our encodingstatic int[]
Reads an int array with our encodingstatic String[]
Reads an int array with our encodingstatic boolean
sleepPastFinalModifiedTime
(File[] files) Sleep until after the last last-modified stamp from the files.static int
sourceSuffixLength
(File file) static int
sourceSuffixLength
(String path) static void
throwIaxUnlessCanReadDir
(File dir, String label) static void
throwIaxUnlessCanWriteDir
(File dir, String label) static void
throwIaxUnlessCanWriteFile
(File file, String label) static String
weakNormalize
(String path) Weakly normalize path for comparisons by trimming and changing '\\' to '/'static String
writeAsString
(File file, String contents) Write contents to file, returning null on success or error message otherwise.static void
writeBooleanArray
(boolean[] a, DataOutputStream s) Writes a boolean array with our encodingstatic void
writeIntArray
(int[] a, DataOutputStream s) Writes an int array with our encodingstatic void
writeStringArray
(String[] a, DataOutputStream s) Writes an int array with our encodingstatic int
zipSuffixLength
(File file) static int
zipSuffixLength
(String path)
-
Felddetails
-
DEFAULT_PARENT
default parent directory File when a file has a null parent -
SOURCE_SUFFIXES
unmodifiable List of String source file suffixes (including leading ".") -
ZIP_FILTER
-
ALL
accept all files -
DIRS_AND_WRITABLE_CLASSES
-
aspectjSourceFileFilter
-
-
Methodendetails
-
isZipFile
- Gibt zurück:
- true if file exists and is a zip file
-
zipSuffixLength
- Gibt zurück:
- 0 if file has no zip/jar suffix or 4 otherwise
-
zipSuffixLength
- Gibt zurück:
- 0 if no zip/jar suffix or 4 otherwise
-
hasSourceSuffix
- Gibt zurück:
- true if file path has a source suffix
-
hasSourceSuffix
- Gibt zurück:
- true if path ends with .java or .aj
-
sourceSuffixLength
- Gibt zurück:
- 0 if file has no source suffix or the length of the suffix otherwise
-
sourceSuffixLength
- Gibt zurück:
- 0 if no source suffix or the length of the suffix otherwise
-
canReadDir
- Gibt zurück:
- true if this is a readable directory
-
canReadFile
- Gibt zurück:
- true if this is a readable file
-
canWriteDir
- Gibt zurück:
- true if dir is a writable directory
-
canWriteFile
- Gibt zurück:
- true if this is a writable file
-
throwIaxUnlessCanReadDir
- Löst aus:
IllegalArgumentException
- unless dir is a readable directory
-
throwIaxUnlessCanWriteFile
- Löst aus:
IllegalArgumentException
- unless file is readable and not a directory
-
throwIaxUnlessCanWriteDir
- Löst aus:
IllegalArgumentException
- unless dir is a readable directory
-
getPaths
- Gibt zurück:
- array same length as input, with String paths
-
getPaths
- Gibt zurück:
- array same length as input, with String paths
-
fileToClassName
Extract the name of a class from the path to its file. If the basedir is null, then the class is assumed to be in the default package unless the classFile has one of the top-level suffixes { com, org, java, javax } as a parent directory.- Parameter:
basedir
- the File of the base directory (prefix of classFile)classFile
- the File of the class to extract the name for- Löst aus:
IllegalArgumentException
- if classFile is null or does not end with ".class" or a non-null basedir is not a prefix of classFile
-
normalizedPath
Normalize path for comparisons by rendering absolute, clipping basedir prefix, trimming and changing '\\' to '/'- Parameter:
file
- the File with the path to normalizebasedir
- the File for the prefix of the file to normalize - ignored if null- Gibt zurück:
- "" if null or normalized path otherwise
- Löst aus:
IllegalArgumentException
- if basedir is not a prefix of file
-
flatten
Render a set of files to String as a path by getting absolute paths of each and delimiting with infix.- Parameter:
files
- the File[] to flatten - may be null or emptyinfix
- the String delimiter internally between entries (if null, then use File.pathSeparator). (alias toflatten(getAbsolutePaths(files), infix)
- Gibt zurück:
- String with absolute paths to entries in order, delimited with infix
-
flatten
Flatten File[] to String.- Parameter:
paths
- the String[] of paths to flatten - null ignoredinfix
- the String infix to use - null treated as File.pathSeparator
-
normalizedPath
Normalize path for comparisons by rendering absolute trimming and changing '\\' to '/'- Gibt zurück:
- "" if null or normalized path otherwise
-
weakNormalize
Weakly normalize path for comparisons by trimming and changing '\\' to '/' -
getBestFile
Get best File for the first-readable path in input paths, treating entries prefixed "sp:" as system property keys. Safe to call in static initializers.- Parameter:
paths
- the String[] of paths to check.- Gibt zurück:
- null if not found, or valid File otherwise
-
getBestFile
-
getBestFile
Render as best file, canonical or absolute.- Parameter:
file
- the File to get the best File for (not null)- Gibt zurück:
- File of the best-available path
- Löst aus:
IllegalArgumentException
- if file is null
-
getBestPath
Render as best path, canonical or absolute.- Parameter:
file
- the File to get the path for (not null)- Gibt zurück:
- String of the best-available path
- Löst aus:
IllegalArgumentException
- if file is null
-
getAbsolutePaths
- Gibt zurück:
- array same length as input, with String absolute paths
-
deleteContents
Recursively delete the contents of dir, but not the dir itself- Gibt zurück:
- the total number of files deleted
-
deleteContents
Recursively delete some contents of dir, but not the dir itself. This deletes any subdirectory which is empty after its files are deleted.- Gibt zurück:
- the total number of files deleted
-
deleteContents
Recursively delete some contents of dir, but not the dir itself. If deleteEmptyDirs is true, this deletes any subdirectory which is empty after its files are deleted.- Parameter:
dir
- the File directory (if a file, the the file is deleted)- Gibt zurück:
- the total number of files deleted
-
copyDir
Copy contents of fromDir into toDir- Parameter:
fromDir
- must exist and be readabletoDir
- must exist or be creatable and be writable- Gibt zurück:
- the total number of files copied
- Löst aus:
IOException
-
copyDir
public static int copyDir(File fromDir, File toDir, String fromSuffix, String toSuffix) throws IOException Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any. This silently ignores dirs and files that are not readable but throw IOException for directories that are not writable. This does not clean out the original contents of toDir. (subdirectories are not renamed per directory rules)- Parameter:
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 empty- Gibt zurück:
- the total number of files copied
- Löst aus:
IOException
-
copyDir
public static int copyDir(File fromDir, File toDir, String fromSuffix, String toSuffix, FileFilter delegate) throws IOException Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any. This silently ignores dirs and files that are not readable but throw IOException for directories that are not writable. This does not clean out the original contents of toDir. (subdirectories are not renamed per directory rules) This calls any delegate FilenameFilter to collect any selected file.- Parameter:
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 empty- Gibt zurück:
- the total number of files copied
- Löst aus:
IOException
-
listFiles
Recursively list files in srcDir.- Gibt zurück:
- ArrayList with String paths of File under srcDir (relative to srcDir)
-
listFiles
Recursively list files in srcDir.- Gibt zurück:
- ArrayList with String paths of File under srcDir (relative to srcDir)
-
listClassFiles
Recursively list .class files in specified directory- Gibt zurück:
- List of File objects
-
getBaseDirFiles
Convert String[] paths to File[] as offset of base directory- Parameter:
basedir
- the non-null File base directory for File to create with pathspaths
- the String[] of paths to create- Gibt zurück:
- File[] with same length as paths
-
getBaseDirFiles
Convert String[] paths to File[] as offset of base directory- Parameter:
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 null- Gibt zurück:
- File[] with same length as paths
-
copyFiles
public static File[] copyFiles(File srcDir, String[] relativePaths, File destDir) throws IllegalArgumentException, IOException Copy files from source dir into destination directory, creating any needed directories. This differs from copyDir in not being recursive; each input with the source dir creates a full path. However, if the source is a directory, it is copied as such.- Parameter:
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 to- Löst aus:
IllegalArgumentException
- if input invalid, IOException if operations failIOException
-
copyFile
Copy fromFile to toFile, handling file-file, dir-dir, and file-dir copies.- Parameter:
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)- Löst aus:
IOException
-
ensureParentWritable
Ensure that the parent directory to path can be written. If the path has a null parent, DEFAULT_PARENT is tested. If the path parent does not exist, this tries to create it.- Parameter:
path
- the File path whose parent should be writable- Gibt zurück:
- the File path of the writable parent directory
- Löst aus:
IllegalArgumentException
- if parent cannot be written or path is null.
-
copyValidFiles
Copy file to file.- Parameter:
fromFile
- the File to copy (readable, non-null file)toFile
- the File to copy to (non-null, parent dir exists)- Löst aus:
IOException
-
copyStream
do line-based copying- Löst aus:
IOException
-
copyStream
- Löst aus:
IOException
-
copyStream
- Löst aus:
IOException
-
makeNewChildDir
Make a new child directory of parent- Parameter:
parent
- a File for the parent (writable)child
- a prefix for the child directory- Gibt zurück:
- a File dir that exists with parentDir as the parent file or null
-
getTempDir
Make a new temporary directory in the same directory that the system uses for temporary files, or if that files, in the current directory.- Parameter:
name
- the preferred (simple) name of the directory - may be null.- Gibt zurück:
- File of an existing new temp dir, or null if unable to create
-
getFileURLs
-
getFileURL
Get URL for a File. This appends "/" for directories. prints errors to System.err- Parameter:
file
- the File to convert to URL (not null)
-
writeAsString
Write contents to file, returning null on success or error message otherwise. This tries to make any necessary parent directories first.- Parameter:
file
- the File to write (not null)contents
- the String to write (use "" if null)- Gibt zurück:
- String null on no error, error otherwise
-
readBooleanArray
Reads a boolean array with our encoding- Löst aus:
IOException
-
writeBooleanArray
Writes a boolean array with our encoding- Löst aus:
IOException
-
readIntArray
Reads an int array with our encoding- Löst aus:
IOException
-
writeIntArray
Writes an int array with our encoding- Löst aus:
IOException
-
readStringArray
Reads an int array with our encoding- Löst aus:
IOException
-
writeStringArray
Writes an int array with our encoding- Löst aus:
IOException
-
readAsString
Returns the contents of this file as a String- Löst aus:
IOException
-
readAsLines
-
readAsByteArray
Returns the contents of this file as a byte[]- Löst aus:
IOException
-
readAsByteArray
Reads this input stream and returns contents as a byte[]- Löst aus:
IOException
-
getStreamFromZip
-
lineSeek
public static List<String> lineSeek(String sought, List<String> sources, boolean listAll, PrintStream errorSink) Do line-based search for literal text in source files, returning file:line where found.- Parameter:
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)- Gibt zurück:
- List of String of the form file:line for each found entry (never null, might be empty)
-
lineSeek
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. Any error is rendered to String and returned as the result.- Parameter:
sought
- the String text to seek in the filesourcePath
- the String of paths to the source fileslistAll
- if false, only list first match in filesink
- the List of String entries of the form {sourcePath}:line:column- Gibt zurück:
- String error if any, or add String entries to sink
-
makeOutputStream
- Löst aus:
FileNotFoundException
-
sleepPastFinalModifiedTime
Sleep until after the last last-modified stamp from the files.- Parameter:
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)- Gibt zurück:
- true if succeeded without 100 interrupts
-
makeClasspath
-