Top Inners Fields Constructors Methods
org.aspectj.util

public Class FileUtil

extends Object
Class Inheritance
Imports
java.io.BufferedOutputStream, .BufferedReader, .ByteArrayOutputStream, .DataInputStream, .DataOutputStream, .File, .FileFilter, .FileInputStream, .FileNotFoundException, .FileOutputStream, .FileReader, .FileWriter, .FilenameFilter, .IOException, .InputStream, .OutputStream, .PrintStream, .Reader, .StringReader, .Writer, java.net.MalformedURLException, .URISyntaxException, .URL, java.nio.file.Files, .NoSuchFileException, .Paths, java.util.ArrayList, .Arrays, .Collections, .LinkedList, .List, java.util.zip.ZipEntry, .ZipFile

Authors
Andy Clement, Kris De Volder

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
FileUtil.Pipe

A pipe when run reads from an input stream to an output stream, optionally sleeping between reads.

Field Summary

Modifier and TypeField and Description
public static final FileFilter
ALL

accept all files

public static final FileFilter
public static final File
DEFAULT_PARENT

default parent directory File when a file has a null parent

public static final FileFilter
pack-priv static final String
pack-priv static final int[]
private static final boolean
public static final List<String>
SOURCE_SUFFIXES

unmodifiable List of String source file suffixes (including leading ".")

public static final FileFilter

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

true if this is a readable directory
canReadDir
(File dir)

public static boolean

Returns:

true if this is a readable file
canReadFile
(File file)

public static boolean

Returns:

true if dir is a writable directory
canWriteDir
(File dir)

public static boolean

Returns:

true if this is a writable file
canWriteFile
(File file)

public static int

Returns:

the total number of files copied
copyDir
(File
must exist and be readable
fromDir
,
File
must exist or be creatable and be writable
toDir
)

Copy contents of fromDir into toDir

public static int

Returns:

the total number of files copied
copyDir
(File fromDir, File toDir, final String
select files with this suffix - select all if null or empty
fromSuffix
,
String
replace fromSuffix with toSuffix in the destination file name - ignored if null or empty, appended to name if fromSuffix is null or empty
toSuffix
)

Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any.

public static int

Returns:

the total number of files copied
copyDir
(File fromDir, File toDir, final String
select files with this suffix - select all if null or empty
fromSuffix
,
final String
replace fromSuffix with toSuffix in the destination file name - ignored if null or empty, appended to name if fromSuffix is null or empty
toSuffix
,
final FileFilter delegate)

Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any.

public static void
copyFile(File
the File path of the file or directory to copy - must be readable
fromFile
,
File
the File path of the target file or directory - must be writable (will be created if it does not exist)
toFile
)

Copy fromFile to toFile, handling file-file, dir-dir, and file-dir copies.

public static File[]
copyFiles(File
an existing, readable directory containing relativePaths files
srcDir
,
String[]
a set of paths relative to srcDir to readable File to copy
relativePaths
,
File
an existing, writable directory to copy files to
destDir
)

Copy files from source dir into destination directory, creating any needed directories.

public static void
copyStream(DataInputStream in, PrintStream out)

do line-based copying

public static void
public static void
public static void
copyValidFiles(File
the File to copy (readable, non-null file)
fromFile
,
File
the File to copy to (non-null, parent dir exists)
toFile
)

Copy file to file.

public static int

Returns:

the total number of files deleted
deleteContents
(File dir)

Recursively delete the contents of dir, but not the dir itself

public static int

Returns:

the total number of files deleted
deleteContents
(File dir, FileFilter filter)

Recursively delete some contents of dir, but not the dir itself.

public static int

Returns:

the total number of files deleted
deleteContents
(File
the File directory (if a file, the the file is deleted)
dir
,
FileFilter filter, boolean deleteEmptyDirs)

Recursively delete some contents of dir, but not the dir itself.

public static File

Returns:

the File path of the writable parent directory
ensureParentWritable
(File
the File path whose parent should be writable
path
)

Ensure that the parent directory to path can be written.

public static String
fileToClassName(File
the File of the base directory (prefix of classFile)
basedir
,
File
the File of the class to extract the name for
classFile
)

Extract the name of a class from the path to its file.

public static String

Returns:

String with absolute paths to entries in order, delimited with infix
flatten
(File[]
the File[] to flatten - may be null or empty
files
,
String
the String delimiter internally between entries (if null, then use File.pathSeparator). (alias to flatten(getAbsolutePaths(files), infix)
infix
)

Render a set of files to String as a path by getting absolute paths of each and delimiting with infix.

public static String
flatten(String[]
the String[] of paths to flatten - null ignored
paths
,
String
the String infix to use - null treated as File.pathSeparator
infix
)

Flatten File[] to String.

public static String[]

Returns:

array same length as input, with String absolute paths
getAbsolutePaths
(File[] files)

public static File[]

Returns:

File[] with same length as paths
getBaseDirFiles
(File
the non-null File base directory for File to create with paths
basedir
,
String[]
the String[] of paths to create
paths
)

Convert String[] paths to File[] as offset of base directory

public static File[]

Returns:

File[] with same length as paths
getBaseDirFiles
(File
the non-null File base directory for File to create with paths
basedir
,
String[]
the String[] of paths to create
paths
,
String[]
the String[] of suffixes to limit sources to - ignored if null
suffixes
)

Convert String[] paths to File[] as offset of base directory

public static File

Returns:

null if not found, or valid File otherwise
getBestFile
(String[]
the String[] of paths to check.
paths
)

Get best File for the first-readable path in input paths, treating entries prefixed "sp:" as system property keys.

public static File
getBestFile(String[] paths, boolean mustBeJar)

public static File

Returns:

File of the best-available path
getBestFile
(File
the File to get the best File for (not null)
file
)

Render as best file, canonical or absolute.

public static String

Returns:

String of the best-available path
getBestPath
(File
the File to get the path for (not null)
file
)

Render as best path, canonical or absolute.

public static URL
getFileURL(File
the File to convert to URL (not null)
file
)

Get URL for a File.

public static URL[]
getFileURLs(File[] files)

public static String[]

Returns:

array same length as input, with String paths
getPaths
(File[] files)

public static String[]

Returns:

array same length as input, with String paths
getPaths
(List<File> files)

public static InputStream
public static File

Returns:

File of an existing new temp dir, or null if unable to create
getTempDir
(String
the preferred (simple) name of the directory - may be null.
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.

public static boolean

Returns:

true if file path has a source suffix
hasSourceSuffix
(File file)

public static boolean

Returns:

true if path ends with .java or .aj
hasSourceSuffix
(String path)

private static boolean

Returns:

true if input is not null and contains no path separator
isValidFileName
(String input)

public static boolean

Returns:

true if file exists and is a zip file
isZipFile
(File file)

public static List<String>

Returns:

List of String of the form file:line for each found entry (never null, might be empty)
lineSeek
(String
the String text to seek in the file
sought
,
List<String>
the List of String paths to the source files
sources
,
boolean
if false, only list first match in file
listAll
,
PrintStream
the PrintStream to print any errors to (one per line) (use null to silently ignore errors)
errorSink
)

Do line-based search for literal text in source files, returning file:line where found.

public static String

Returns:

String error if any, or add String entries to sink
lineSeek
(String
the String text to seek in the file
sought
,
String
the String of paths to the source files
sourcePath
,
boolean
if false, only list first match in file
listAll
,
List<String>
the List of String entries of the form {sourcePath}:line:column
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.

public static List<File>

Returns:

List of File objects
listClassFiles
(File dir)

Recursively list .class files in specified directory

private static void
listClassFiles(final File baseDir, ArrayList<File> result)

public static String[]

Returns:

ArrayList with String paths of File under srcDir (relative to srcDir)
listFiles
(File srcDir)

Recursively list files in srcDir.

public static File[]

Returns:

ArrayList with String paths of File under srcDir (relative to srcDir)
listFiles
(File srcDir, FileFilter fileFilter)

Recursively list files in srcDir.

private static void
listFiles(final File baseDir, ArrayList<File> result, FileFilter filter)

private static void
listFiles(final File baseDir, String dir, ArrayList<String> result)

public static List<String>
public static File

Returns:

a File dir that exists with parentDir as the parent file or null
makeNewChildDir
(File
a File for the parent (writable)
parent
,
String
a prefix for the child directory
child
)

Make a new child directory of parent

public static BufferedOutputStream
private static File

Returns:

File for the new file.
newFile
(File
the File for the parent directory of the file
dir
,
String
the path in the parent directory (filename only?)
path
)

Create a new File, resolving paths ".." and "." specially.

public static String

Returns:

"" if null or normalized path otherwise
normalizedPath
(File
the File with the path to normalize
file
,
File
the File for the prefix of the file to normalize - ignored if null
basedir
)

Normalize path for comparisons by rendering absolute, clipping basedir prefix, trimming and changing '\\' to '/'

public static String

Returns:

"" if null or normalized path otherwise
normalizedPath
(File file)

Normalize path for comparisons by rendering absolute trimming and changing '\\' to '/'

pack-priv static String

Returns:

semi-random String of length 6 usable as filename suffix
randomFileString
()

public static byte[]
readAsByteArray(File file)

Returns the contents of this file as a byte[]

public static byte[]
readAsByteArray(InputStream inStream)

Reads this input stream and returns contents as a byte[]

public static List<String>
public static String
readAsString(File file)

Returns the contents of this file as a String

public static boolean[]
readBooleanArray(DataInputStream s)

Reads a boolean array with our encoding

public static int[]
readIntArray(DataInputStream s)

Reads an int array with our encoding

public static String[]
readStringArray(DataInputStream s)

Reads an int array with our encoding

public static boolean

Returns:

true if succeeded without 100 interrupts
sleepPastFinalModifiedTime
(File[]
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)
files
)

Sleep until after the last last-modified stamp from the files.

public static int

Returns:

0 if file has no source suffix or the length of the suffix otherwise
sourceSuffixLength
(File file)

public static int

Returns:

0 if no source suffix or the length of the suffix otherwise
sourceSuffixLength
(String path)

public static void
public static void
public static void
private static String
public static String
weakNormalize(String path)

Weakly normalize path for comparisons by trimming and changing '\\' to '/'

public static String

Returns:

String null on no error, error otherwise
writeAsString
(File
the File to write (not null)
file
,
String
the String to write (use "" if null)
contents
)

Write contents to file, returning null on success or error message otherwise.

public static void
writeBooleanArray(boolean[] a, DataOutputStream s)

Writes a boolean array with our encoding

public static void
writeIntArray(int[] a, DataOutputStream s)

Writes an int array with our encoding

public static void
writeStringArray(String[] a, DataOutputStream s)

Writes an int array with our encoding

public static int

Returns:

0 if file has no zip/jar suffix or 4 otherwise
zipSuffixLength
(File file)

public static int

Returns:

0 if no zip/jar suffix or 4 otherwise
zipSuffixLength
(String path)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait