Class FileUtils

  • Direct Known Subclasses:
    FileUtils

    public class FileUtils
    extends java.lang.Object
    Some file utility methods (may partially be taken from commons.io but this is currently no defined as dependency of this bundle - see standalone).
    Author:
    Holger Eichelberger
    • Constructor Summary

      Constructors 
      Constructor Description
      FileUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void closeQuietly​(java.io.Closeable closeable)
      Closes a closeable quietly, i.e., without exception.
      static void copyFile​(java.io.File target, java.io.File source)
      Copies a file.
      static void copyToFile​(java.io.File file, java.io.InputStream in)
      Copies the contents of in at the current position to file.
      static void copyToFile​(java.io.File file, java.io.InputStream in, byte[] buffer)
      Copies the contents of in at the current position to file.
      static void createIfNotExists​(java.io.File file)
      Creates a (physical) file if it does not exist.
      static java.io.File createTmpDir​(java.lang.String name)
      Creates a temporary directory and deletes an existing one if it already exists.
      static java.io.File createTmpDir​(java.lang.String name, boolean deleteIfExists)
      Creates a temporary directory.
      static boolean isFileURI​(java.net.URI uri)
      Returns whether the given uri is a file URI as returned, e.g., by a File.
      static boolean isFileURL​(java.net.URL url)
      Returns whether the given url is a JAR URL as returned, e.g., by a classloader.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileUtils

        public FileUtils()
    • Method Detail

      • isFileURI

        public static boolean isFileURI​(java.net.URI uri)
        Returns whether the given uri is a file URI as returned, e.g., by a File.
        Parameters:
        uri - the URI (ignored if null)
        Returns:
        true for a file URI, false else
      • isFileURL

        public static boolean isFileURL​(java.net.URL url)
        Returns whether the given url is a JAR URL as returned, e.g., by a classloader.
        Parameters:
        url - the URL (ignored if null)
        Returns:
        true for a JAR URL, false else
      • copyFile

        public static void copyFile​(java.io.File target,
                                    java.io.File source)
                             throws java.io.IOException
        Copies a file.
        Parameters:
        target - the target file
        source - the source file
        Throws:
        java.io.IOException - in any case of I/O problem, trying to close the involved files before end if possible
      • copyToFile

        public static void copyToFile​(java.io.File file,
                                      java.io.InputStream in)
                               throws java.io.IOException
        Copies the contents of in at the current position to file. Does not attempt to close in. Uses a default buffer of size 1024.
        Parameters:
        file - the file to write to
        in - the input stream
        Throws:
        java.io.IOException - in any case of I/O problem, trying to close file before end if possible
      • copyToFile

        public static void copyToFile​(java.io.File file,
                                      java.io.InputStream in,
                                      byte[] buffer)
                               throws java.io.IOException
        Copies the contents of in at the current position to file. Does not attempt to close in. Uses a default buffer of size 1024.
        Parameters:
        file - the file to write to
        in - the input stream
        buffer - a copy buffer (if null a default one of size 1024 is used)
        Throws:
        java.io.IOException - in any case of I/O problem, trying to close file before if possible
      • closeQuietly

        public static void closeQuietly​(java.io.Closeable closeable)
        Closes a closeable quietly, i.e., without exception.
        Parameters:
        closeable - the closeable
      • createIfNotExists

        public static void createIfNotExists​(java.io.File file)
        Creates a (physical) file if it does not exist.
        Parameters:
        file - the file (may be null, ignored then)
      • createTmpDir

        public static java.io.File createTmpDir​(java.lang.String name)
        Creates a temporary directory and deletes an existing one if it already exists. Deletion on exit of JVM is enabled for the resulting directory.
        Parameters:
        name - the name of the directory within the standard temporary folder
        Returns:
        the temporary directory
      • createTmpDir

        public static java.io.File createTmpDir​(java.lang.String name,
                                                boolean deleteIfExists)
        Creates a temporary directory. Deletion on exit of JVM is enabled for the resulting directory.
        Parameters:
        name - the name of the directory within the standard temporary folder
        deleteIfExists - delete an existing one if it already exists
        Returns:
        the temporary directory