Class FileUtils

java.lang.Object
org.archive.util.FileUtils

public class FileUtils extends Object
Utility methods for manipulating files and directories.
Author:
John Erik Halse, gojomo
  • Field Details

  • Method Details

    • copyFile

      public static boolean copyFile(File src, File dest) throws FileNotFoundException, IOException
      Copy the src file to the destination. Deletes any preexisting file at destination.
      Parameters:
      src -
      dest -
      Returns:
      True if the extent was greater than actual bytes copied.
      Throws:
      FileNotFoundException
      IOException
    • copyFile

      public static boolean copyFile(File src, File dest, long extent) throws FileNotFoundException, IOException
      Copy up to extent bytes of the source file to the destination. Deletes any preexisting file at destination.
      Parameters:
      src -
      dest -
      extent - Maximum number of bytes to copy
      Returns:
      True if the extent was greater than actual bytes copied.
      Throws:
      FileNotFoundException
      IOException
    • copyFile

      public static boolean copyFile(File src, File dest, long extent, boolean overwrite) throws FileNotFoundException, IOException
      Copy up to extent bytes of the source file to the destination
      Parameters:
      src -
      dest -
      extent - Maximum number of bytes to copy
      overwrite - If target file already exits, and this parameter is true, overwrite target file (We do this by first deleting the target file before we begin the copy).
      Returns:
      True if the extent was greater than actual bytes copied.
      Throws:
      FileNotFoundException
      IOException
    • workaroundCopyFile

      protected static void workaroundCopyFile(File src, File dest) throws IOException
      Throws:
      IOException
    • getFilesWithPrefix

      public static File[] getFilesWithPrefix(File dir, String prefix)
      Get a list of all files in directory that have passed prefix.
      Parameters:
      dir - Dir to look in.
      prefix - Basename of files to look for. Compare is case insensitive.
      Returns:
      List of files in dir that start w/ passed basename.
    • getRegexFileFilter

      public static org.apache.commons.io.filefilter.IOFileFilter getRegexFileFilter(String regex)
      Get a @link java.io.FileFilter that filters files based on a regular expression.
      Parameters:
      regex - the regular expression the files must match.
      Returns:
      the newly created filter.
    • assertReadable

      public static File assertReadable(File f) throws FileNotFoundException
      Test file exists and is readable.
      Parameters:
      f - File to test.
      Throws:
      FileNotFoundException - If file does not exist or is not unreadable.
    • isReadableWithExtensionAndMagic

      public static boolean isReadableWithExtensionAndMagic(File f, String uncompressedExtension, String magic) throws IOException
      Parameters:
      f - File to test.
      Returns:
      True if file is readable, has uncompressed extension, and magic string at file start.
      Throws:
      IOException - If file not readable or other problem.
    • maybeRelative

      public static File maybeRelative(File context, String path)
      Turn path into a File, relative to context (which may be ignored if path is absolute).
      Parameters:
      context - File context if path is relative
      path - String path to make into a File
      Returns:
      File created
    • loadProperties

      public static Properties loadProperties(File file) throws IOException
      Load Properties instance from a File
      Parameters:
      file -
      Returns:
      Properties
      Throws:
      IOException
    • storeProperties

      public static void storeProperties(Properties p, File file) throws IOException
      Store Properties instance to a File
      Parameters:
      p -
      file - destination File
      Throws:
      IOException
    • moveAsideIfExists

      public static boolean moveAsideIfExists(File file) throws IOException
      Throws:
      IOException
    • pagedLines

      public static org.apache.commons.lang3.LongRange pagedLines(File file, long position, int signedDesiredLineCount, List<String> lines, int lineEstimate) throws IOException
      Retrieve a number of lines from the file around the given position, as when paging forward or backward through a file.
      Parameters:
      file - File to retrieve lines
      position - offset to anchor lines
      signedDesiredLineCount - lines requested; if negative, want this number of lines ending with a line containing the position; if positive, want this number of lines, all starting at or after position.
      lines - list to insert found lines
      lineEstimate - int estimate of line size, 0 means use default of 128
      Returns:
      LongRange indicating the file offsets corresponding to the beginning of the first line returned, and the point after the end of the last line returned
      Throws:
      IOException
    • expandRange

      public static org.apache.commons.lang3.LongRange expandRange(org.apache.commons.lang3.LongRange range1, org.apache.commons.lang3.LongRange range2)
    • pagedLines

      public static org.apache.commons.lang3.LongRange pagedLines(File file, long position, int signedDesiredLongCount, List<String> lines) throws IOException
      Throws:
      IOException
    • deleteSoonerOrLater

      public static void deleteSoonerOrLater(File fileToDelete)
      Delete the file now -- but in the event of failure, keep trying in the future. VERY IMPORTANT: Do not use with any file whose name/path may be reused, because the lagged delete could then wind up deleting the newer file. Essentially, only to be used with uniquely-named temp files. Necessary because some platforms (looking at you, JVM-on-Windows) will have deletes fail because of things like file-mapped buffers remaining, and there's no explicit way to unmap a buffer. (See 6-year-old Sun-stumping Java bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038 ) We just have to wait and retry. (Why not just File.deleteOnExit? There could be an arbitrary, unbounded number of files in such a situation, that are only deletable a few seconds or minutes after our first attempt. Waiting for JVM exist could mean disk exhaustion. It's also unclear if the native FS class implementations of deleteOnExit use RAM per pending file.)
      Parameters:
      fileToDelete -
    • readFullyToFile

      public static long readFullyToFile(InputStream is, File toFile) throws IOException
      Read the entire stream to EOF into the passed file. Closes is when done or if an exception.
      Parameters:
      is - Stream to read.
      toFile - File to write to.
      Throws:
      IOException
    • ensureWriteableDirectory

      public static File ensureWriteableDirectory(String dir) throws IOException
      Ensure writeable directory. If doesn't exist, we attempt creation.
      Parameters:
      dir - Directory to test for exitence and is writeable.
      Returns:
      The passed dir.
      Throws:
      IOException - If passed directory does not exist and is not createable, or directory is not writeable or is not a directory.
    • ensureWriteableDirectory

      public static List<File> ensureWriteableDirectory(List<File> dirs) throws IOException
      Ensure writeable directories. If doesn't exist, we attempt creation.
      Parameters:
      dirs - List of Files to test.
      Returns:
      The passed dirs.
      Throws:
      IOException - If passed directory does not exist and is not createable, or directory is not writeable or is not a directory.
    • ensureWriteableDirectory

      public static File ensureWriteableDirectory(File dir) throws IOException
      Ensure writeable directory. If doesn't exist, we attempt creation.
      Parameters:
      dir - Directory to test for exitence and is writeable.
      Returns:
      The passed dir.
      Throws:
      IOException - If passed directory does not exist and is not createable, or directory is not writeable or is not a directory.
    • tryToCanonicalize

      public static File tryToCanonicalize(File file)
    • appendTo

      public static void appendTo(File fileToAppendTo, File fileToAppendFrom) throws IOException
      Throws:
      IOException