Class FileUtil

java.lang.Object
org.cip4.jdflib.util.FileUtil

public class FileUtil extends Object
collection of helper routines to work with files
  • Method Details

    • isLocked

      public static boolean isLocked(File file)
    • exists

      public static boolean exists(File file)
    • isFile

      public static boolean isFile(File file)
    • getAuxDir

      public static File getAuxDir(File hotFile)
      get any auxiliary directory with the same name as a file
      Parameters:
      hotFile -
      Returns:
    • listFilesWithExtension

      public static File[] listFilesWithExtension(File dir, String extension, int max)
      list all files with a given extension (directories are skipped
      Parameters:
      dir - the directory to search
      extension - comma separated list of extensions to check for (null = list all)
      Returns:
      Files[] the matching files, null if none are found
    • listFilesWithExtension

      public static File[] listFilesWithExtension(File dir, String allExtensions)
    • listFilesWithExpression

      public static File[] listFilesWithExpression(File dir, String expression)
      list all files matching given regexp
      Parameters:
      dir - the directory to search
      expression - regular expression - uses the simplified syntax
      Returns:
      Files[] the matching files, null if none are found
    • listFilesWithExpression

      public static File[] listFilesWithExpression(File dir, String expression, int max)
      list all files matching given regexp
      Parameters:
      dir - the directory to search
      expression - regular expression - uses the simplified syntax
      Returns:
      Files[] the matching files, null if none are found
    • numFiles

      public static int numFiles(File dir, int max)
      number of files - at most max
      Parameters:
      dir - the directory to search
      expression - regular expression - uses the simplified syntax
      Returns:
      Files[] the matching files, null if none are found
    • listFilesInTree

      public static Vector<File> listFilesInTree(File dir, FileFilter filter)
      list all files matching given regexp
      Parameters:
      dir - the directory to search
      filter - the filter to apply to files
      Returns:
      Vector the matching files, null if none are found
    • listFilesInTree

      public static Vector<File> listFilesInTree(File dir, String expression)
      list all files matching given regexp
      Parameters:
      dir - the directory to search
      expression - comma separated list of regular expression of a tree with slashes separating directories
      Returns:
      Files[] the matching files, null if none are found
    • listDirectories

      public static File[] listDirectories(File dir)
      list all direct child directories
      Parameters:
      dir - the directory to search
      Returns:
      Files[] the matching directories, null if none are found
    • deleteAll

      public static boolean deleteAll(File dirToZapp)
      very brutal tree zapper that will delete a file or directory tree recursively
      Parameters:
      dirToZapp - the file directory to utterly anihilate
      Returns:
      true if ciao
    • streamToFile

      public static File streamToFile(InputStream fis, String fileName)
      dump a stream to a newly created file
      Parameters:
      fis - the inputstream to read
      fileName - the file to stream to
      Returns:
      the file created by the stream, null if snafu
    • writeFile

      public static File writeFile(IStreamWriter w, File file)
      write to a file
      Parameters:
      file - the file to write
      w - the writer to write to
      Returns:
      the file that was created, null if snafu
    • newExtension

      public static File newExtension(File f, String newExt)
      create a File object with a new extension
      Parameters:
      f - the file, if null always returns null
      newExt - the new extension
      Returns:
      the file with the new extension
      See Also:
    • streamToFile

      public static File streamToFile(InputStream fis, File fil)
      Parameters:
      fis - the InputStream to read - if null nothing happens
      fil - the file to stream to
      Returns:
      the file created by the stream, null if snafu
    • stringToFile

      public static File stringToFile(String s, File fil)
      Parameters:
      s - the String to read - if null nothing happens
      fil - the file to stream to
      Returns:
      the file created by the stream, null if snafu
    • getFastMD5

      public static byte[] getFastMD5(File f, int maxSize)
      get an md5 from a file that reads at most 2*maxSize bytes of which maxSize are from the front and maxSize are from the back
      Parameters:
      f -
      maxSize - if<=0 always use entir length
      Returns:
    • streamToMD5File

      public static MyPair<File,byte[]> streamToMD5File(InputStream fis, File fil)
      same as streanToFile but also calculates the md5 hash of the stream
      Parameters:
      fis - the InputStream to read - if null nothing happens
      fil - the file to stream to
      Returns:
      the file created by the stream, null if snafu
    • fileToString

      public static String fileToString(File file, Charset s)
      read a file into a byte array
      Parameters:
      file - the file to read into a byte array
      Returns:
      the correctly sized byte array, null if no bytes were read
    • fileToByteArray

      public static byte[] fileToByteArray(File file)
      read a file into a byte array
      Parameters:
      file - the file to read into a byte array
      Returns:
      the correctly sized byte array, null if no bytes were read
    • moveFileToDir

      public static File moveFileToDir(File fromFile, File toDir)
      moves a File to directory by trying to rename, if this fails, a copy with subsequent delete is performed. if toFile exists, it is brutally overwritten
      Parameters:
      fromFile - the File to move
      toDir - the Directory to move to
      Returns:
      File the moved File if success, else null, i.e. toFile exists with the contents of fromFile
    • moveFile

      public static boolean moveFile(File fromFile, File toFile)
      moves a File by trying to rename, if this fails, a copy with subsequent delete is performed. if toFile exists, it is brutally overwritten
      Parameters:
      fromFile - the File to move
      toFile - the File to create
      Returns:
      boolean true if success, i.e. toFile exists with the contents of fromFile
    • copyBytes

      public static boolean copyBytes(byte[] buf, File toFile)
      copy a buffer to the end of a file, creating it if necessary
      Parameters:
      buf - the source buffer
      toFile - the destination File
      Returns:
      true if success
    • copyFile

      public static boolean copyFile(File fromFile, File toFile)
      copy a file, unless fromFile and toFile are equal
      Parameters:
      fromFile - the source File
      toFile - the destination File
      Returns:
      true if success
    • ensureFileInDir

      public static File ensureFileInDir(File fromFile, File toDir)
      copies a File to directory if and only if toFile does not exist
      Parameters:
      fromFile - the File to move
      toDir - the Directory to move to
      Returns:
      File the destination File if success,
    • cleanDots

      public static File cleanDots(File file)
      remove any internal "../" "./" and "//" from a url
      Parameters:
      file - the file to clean
      Returns:
      File - the clean file
    • copyFileToDir

      public static File copyFileToDir(File fromFile, File toDir)
      copies a File to directory if toFile exists, it is brutally overwritten unless fromFile equals toFile
      Parameters:
      fromFile - the File to move
      toDir - the Directory to move to
      Returns:
      File the moved File if success, else null, i.e. toFile exists with the contents of fromFile
    • getFileInDirectory

      public static File getFileInDirectory(File dir, File localFile)
      returns a File object corresponding to an instance of localFile placed in dir - No OS calls are made and File is NOT created
      Parameters:
      dir - the File Object representing the directory
      localFile - the local file to place in dir, note that only the path is copied - this does copy trees
      Returns:
      File the File object that represents localFile in Dir
    • addSecure

      public static File addSecure(File baseFile, File file) throws IllegalArgumentException
      securely add a subdirectory
      Parameters:
      baseFile -
      file -
      Returns:
      Throws:
      IllegalArgumentException
    • getSecureName

      public static String getSecureName(File file) throws IllegalArgumentException
      secure check of a file name
      Parameters:
      file -
      Returns:
      Throws:
      IllegalArgumentException
    • getSecureFileName

      public static String getSecureFileName(File file)
      secure check of a file name
      Parameters:
      file -
      Returns:
      Throws:
      IllegalArgumentException
    • getSecurePath

      public static String getSecurePath(File file, boolean allowAbsolute) throws IllegalArgumentException
      secure check of a file path
      Parameters:
      baseFile -
      file -
      Returns:
      Throws:
      IllegalArgumentException
    • forceDelete

      public static boolean forceDelete(File file)
      forces deletion of a file
      Parameters:
      file - the file to delete
      Returns:
      true if the file no longer exists
    • forceDelete

      public static boolean forceDelete(File file, int loops)
      forces deletion of a file
      Parameters:
      file - the file to delete
      Returns:
      true if the file no longer exists
    • getCreateDirectory

      public static File getCreateDirectory(String newDir)
      create a new directory and return null if the directory could not be created
      Parameters:
      newDir - the path or URL of the new directory
      Returns:
    • cleanURL

      public static File cleanURL(File f)
      Parameters:
      f - the file to cleanup
      Returns:
      the cleaned file
    • isDirectory

      public static boolean isDirectory(File f)
      check whether a file is a directory. If a unix file is a symbolic link to a directory, it is also assumed to be a directory
      Parameters:
      f - the file to test
      Returns:
      true if f is a directory or link to a directory
    • isDirectory

      public static boolean isDirectory(String s)
      check whether a file is a directory. If a unix file is a symbolic link to a directory, it is also assumed to be a directory
      Parameters:
      s - the file to test
      Returns:
      true if f is a directory or link to a directory
    • isAbsoluteFile

      public static boolean isAbsoluteFile(File f)
      check whether a file is absolute
      Parameters:
      f - the file to test
      Returns:
      true if f is absolute;
    • isAbsoluteFile

      public static boolean isAbsoluteFile(String f)
      Parameters:
      f - the file path to test
      Returns:
      true if s is absolute
    • getExtension

      public static String getExtension(File file)
      Parameters:
      file -
      Returns:
      the file extension
    • createNewFile

      public static boolean createNewFile(File file)
      similar to File.createFile but also creates any required directories
      Parameters:
      file - the file to create
      Returns:
      true if the file exists after the call, else false
    • createFile

      public static boolean createFile(File file) throws IOException
      similar to File.createFile but also creates any required directories
      Parameters:
      file - the file to create
      Returns:
      true if the file exists after the call, else false
      Throws:
      IOException
    • ensureParent

      public static boolean ensureParent(File file)
    • equals

      public static boolean equals(File file1, File file2)
      checks the equivalence of files - todo os specific behavior (just in case)
      Parameters:
      file1 -
      file2 -
      Returns:
    • getBufferedInputStream

      public static BufferedInputStream getBufferedInputStream(File file)
      create a buffered input stream for a file
      Parameters:
      file -
      Returns:
      the buffered input stream, null if snafu
    • getBufferedOutputStream

      public static BufferedOutputStream getBufferedOutputStream(File file)
      create a buffered output stream for a file
      Parameters:
      file -
      Returns:
      the buffered output stream, null if snafu
    • getBufferedOutputStream

      public static BufferedOutputStream getBufferedOutputStream(File file, boolean append)
      create a buffered output stream for a file
      Parameters:
      file -
      append -
      Returns:
      the buffered output stream, null if snafu
    • isWindows

      @Deprecated public static boolean isWindows()
      Deprecated.
      Returns:
      true if we are on a windows file system
    • dumpException

      public static void dumpException(File except, Throwable t)