Klasse FileUtil

java.lang.Object
org.aspectj.util.FileUtil

public class FileUtil extends Object
Autor:
Andy Clement, Kris De Volder
  • Felddetails

    • DEFAULT_PARENT

      public static final File DEFAULT_PARENT
      default parent directory File when a file has a null parent
    • SOURCE_SUFFIXES

      public static final List<String> SOURCE_SUFFIXES
      unmodifiable List of String source file suffixes (including leading ".")
    • ZIP_FILTER

      public static final FileFilter ZIP_FILTER
    • ALL

      public static final FileFilter ALL
      accept all files
    • DIRS_AND_WRITABLE_CLASSES

      public static final FileFilter DIRS_AND_WRITABLE_CLASSES
    • aspectjSourceFileFilter

      public static final FileFilter aspectjSourceFileFilter
  • Methodendetails

    • isZipFile

      public static boolean isZipFile(File file)
      Gibt zurück:
      true if file exists and is a zip file
    • zipSuffixLength

      public static int zipSuffixLength(File file)
      Gibt zurück:
      0 if file has no zip/jar suffix or 4 otherwise
    • zipSuffixLength

      public static int zipSuffixLength(String path)
      Gibt zurück:
      0 if no zip/jar suffix or 4 otherwise
    • hasSourceSuffix

      public static boolean hasSourceSuffix(File file)
      Gibt zurück:
      true if file path has a source suffix
    • hasSourceSuffix

      public static boolean hasSourceSuffix(String path)
      Gibt zurück:
      true if path ends with .java or .aj
    • sourceSuffixLength

      public static int sourceSuffixLength(File file)
      Gibt zurück:
      0 if file has no source suffix or the length of the suffix otherwise
    • sourceSuffixLength

      public static int sourceSuffixLength(String path)
      Gibt zurück:
      0 if no source suffix or the length of the suffix otherwise
    • canReadDir

      public static boolean canReadDir(File dir)
      Gibt zurück:
      true if this is a readable directory
    • canReadFile

      public static boolean canReadFile(File file)
      Gibt zurück:
      true if this is a readable file
    • canWriteDir

      public static boolean canWriteDir(File dir)
      Gibt zurück:
      true if dir is a writable directory
    • canWriteFile

      public static boolean canWriteFile(File file)
      Gibt zurück:
      true if this is a writable file
    • throwIaxUnlessCanReadDir

      public static void throwIaxUnlessCanReadDir(File dir, String label)
      Löst aus:
      IllegalArgumentException - unless dir is a readable directory
    • throwIaxUnlessCanWriteFile

      public static void throwIaxUnlessCanWriteFile(File file, String label)
      Löst aus:
      IllegalArgumentException - unless file is readable and not a directory
    • throwIaxUnlessCanWriteDir

      public static void throwIaxUnlessCanWriteDir(File dir, String label)
      Löst aus:
      IllegalArgumentException - unless dir is a readable directory
    • getPaths

      public static String[] getPaths(File[] files)
      Gibt zurück:
      array same length as input, with String paths
    • getPaths

      public static String[] getPaths(List<File> files)
      Gibt zurück:
      array same length as input, with String paths
    • fileToClassName

      public static String fileToClassName(File basedir, File classFile)
      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

      public static String normalizedPath(File file, File basedir)
      Normalize path for comparisons by rendering absolute, clipping basedir prefix, trimming and changing '\\' to '/'
      Parameter:
      file - the File with the path to normalize
      basedir - 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

      public static String flatten(File[] files, String infix)
      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 empty
      infix - the String delimiter internally between entries (if null, then use File.pathSeparator). (alias to flatten(getAbsolutePaths(files), infix)
      Gibt zurück:
      String with absolute paths to entries in order, delimited with infix
    • flatten

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

      public static String normalizedPath(File file)
      Normalize path for comparisons by rendering absolute trimming and changing '\\' to '/'
      Gibt zurück:
      "" if null or normalized path otherwise
    • weakNormalize

      public static String weakNormalize(String path)
      Weakly normalize path for comparisons by trimming and changing '\\' to '/'
    • getBestFile

      public static File getBestFile(String[] paths)
      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

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

      public static File getBestFile(File file)
      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

      public static String getBestPath(File file)
      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

      public static String[] getAbsolutePaths(File[] files)
      Gibt zurück:
      array same length as input, with String absolute paths
    • deleteContents

      public static int deleteContents(File dir)
      Recursively delete the contents of dir, but not the dir itself
      Gibt zurück:
      the total number of files deleted
    • deleteContents

      public static int deleteContents(File dir, FileFilter filter)
      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

      public static int deleteContents(File dir, FileFilter filter, boolean deleteEmptyDirs)
      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

      public static int copyDir(File fromDir, File toDir) throws IOException
      Copy contents of fromDir into toDir
      Parameter:
      fromDir - must exist and be readable
      toDir - 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 empty
      toSuffix - 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 empty
      toSuffix - 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

      public static String[] listFiles(File srcDir)
      Recursively list files in srcDir.
      Gibt zurück:
      ArrayList with String paths of File under srcDir (relative to srcDir)
    • listFiles

      public static File[] listFiles(File srcDir, FileFilter fileFilter)
      Recursively list files in srcDir.
      Gibt zurück:
      ArrayList with String paths of File under srcDir (relative to srcDir)
    • listClassFiles

      public static List<File> listClassFiles(File dir)
      Recursively list .class files in specified directory
      Gibt zurück:
      List of File objects
    • getBaseDirFiles

      public static File[] getBaseDirFiles(File basedir, String[] paths)
      Convert String[] paths to File[] as offset of base directory
      Parameter:
      basedir - the non-null File base directory for File to create with paths
      paths - the String[] of paths to create
      Gibt zurück:
      File[] with same length as paths
    • getBaseDirFiles

      public static File[] getBaseDirFiles(File basedir, String[] paths, String[] suffixes)
      Convert String[] paths to File[] as offset of base directory
      Parameter:
      basedir - the non-null File base directory for File to create with paths
      paths - the String[] of paths to create
      suffixes - 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 files
      relativePaths - a set of paths relative to srcDir to readable File to copy
      destDir - an existing, writable directory to copy files to
      Löst aus:
      IllegalArgumentException - if input invalid, IOException if operations fail
      IOException
    • copyFile

      public static void copyFile(File fromFile, File toFile) throws IOException
      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 readable
      toFile - 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

      public static File ensureParentWritable(File path)
      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

      public static void copyValidFiles(File fromFile, File toFile) throws IOException
      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

      public static void copyStream(DataInputStream in, PrintStream out) throws IOException
      do line-based copying
      Löst aus:
      IOException
    • copyStream

      public static void copyStream(InputStream in, OutputStream out) throws IOException
      Löst aus:
      IOException
    • copyStream

      public static void copyStream(Reader in, Writer out) throws IOException
      Löst aus:
      IOException
    • makeNewChildDir

      public static File makeNewChildDir(File parent, String child)
      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

      public 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.
      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

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

      public static URL getFileURL(File file)
      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

      public static String writeAsString(File file, String contents)
      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

      public static boolean[] readBooleanArray(DataInputStream s) throws IOException
      Reads a boolean array with our encoding
      Löst aus:
      IOException
    • writeBooleanArray

      public static void writeBooleanArray(boolean[] a, DataOutputStream s) throws IOException
      Writes a boolean array with our encoding
      Löst aus:
      IOException
    • readIntArray

      public static int[] readIntArray(DataInputStream s) throws IOException
      Reads an int array with our encoding
      Löst aus:
      IOException
    • writeIntArray

      public static void writeIntArray(int[] a, DataOutputStream s) throws IOException
      Writes an int array with our encoding
      Löst aus:
      IOException
    • readStringArray

      public static String[] readStringArray(DataInputStream s) throws IOException
      Reads an int array with our encoding
      Löst aus:
      IOException
    • writeStringArray

      public static void writeStringArray(String[] a, DataOutputStream s) throws IOException
      Writes an int array with our encoding
      Löst aus:
      IOException
    • readAsString

      public static String readAsString(File file) throws IOException
      Returns the contents of this file as a String
      Löst aus:
      IOException
    • readAsLines

      public static List<String> readAsLines(File file)
    • readAsByteArray

      public static byte[] readAsByteArray(File file) throws IOException
      Returns the contents of this file as a byte[]
      Löst aus:
      IOException
    • readAsByteArray

      public static byte[] readAsByteArray(InputStream inStream) throws IOException
      Reads this input stream and returns contents as a byte[]
      Löst aus:
      IOException
    • getStreamFromZip

      public static InputStream getStreamFromZip(String zipFile, String name)
    • 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 file
      sources - the List of String paths to the source files
      listAll - if false, only list first match in file
      errorSink - 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

      public static String lineSeek(String sought, String sourcePath, boolean listAll, List<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. Any error is rendered to String and returned as the result.
      Parameter:
      sought - the String text to seek in the file
      sourcePath - the String of paths to the source files
      listAll - if false, only list first match in file
      sink - 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

      public static BufferedOutputStream makeOutputStream(File file) throws FileNotFoundException
      Löst aus:
      FileNotFoundException
    • sleepPastFinalModifiedTime

      public static boolean sleepPastFinalModifiedTime(File[] files)
      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

      public static List<String> makeClasspath(URL[] urls)