Class FileUtils

    • Method Detail

      • deleteDirectory

        public static void deleteDirectory​(File directory)
                                    throws IOException
        Deletes a directory recursively.
        Parameters:
        directory - directory to delete
        Throws:
        IOException - in case deletion is unsuccessful
      • isSymlink

        public static boolean isSymlink​(File file)
                                 throws IOException
        Determines whether the specified file is a Symbolic Link rather than an actual file.

        Will not return true if there is a Symbolic Link anywhere in the path, only if the specific file is.

        Note: the current implementation always returns false if the system is detected as Windows

        For code that runs on Java 1.7 or later, use the following method instead:
        boolean java.nio.file.Files.isSymbolicLink(Path path)

        Parameters:
        file - the file to check
        Returns:
        true if the file is a Symbolic Link
        Throws:
        IOException - if an IO error occurs while checking the file
        Since:
        2.0
      • cleanDirectory

        public static void cleanDirectory​(File directory)
                                   throws IOException
        Deletes a directory recursively.
        Parameters:
        directory - directory to delete
        Throws:
        IOException - in case deletion is unsuccessful
      • forceDelete

        public static void forceDelete​(File file)
                                throws IOException
        Deletes a file. If file is a directory, delete it and all sub-directories.

        The difference between File.delete() and this method are:

        • A directory to be deleted does not have to be empty.
        • You get exceptions when a file or directory cannot be deleted. (java.io.File methods returns a boolean)
        Parameters:
        file - file or directory to delete, must not be null
        Throws:
        NullPointerException - if the directory is null
        FileNotFoundException - if the file was not found
        IOException - in case deletion is unsuccessful
      • getTempDirectoryPath

        public static String getTempDirectoryPath()
        Returns the path to the system temporary directory.
        Returns:
        the path to the system temporary directory.
        Since:
        2.0
      • readFileToString

        public static String readFileToString​(File file,
                                              Charset encoding)
                                       throws IOException
        Reads the contents of a file into a String. The file is always closed.
        Parameters:
        file - the file to read, must not be null
        encoding - the encoding to use, null means platform default
        Returns:
        the file contents, never null
        Throws:
        IOException - in case of an I/O error
        Since:
        2.3
      • readFileToString

        public static String readFileToString​(File file,
                                              String encoding)
                                       throws IOException
        Reads the contents of a file into a String. The file is always closed.
        Parameters:
        file - the file to read, must not be null
        encoding - the encoding to use, null means platform default
        Returns:
        the file contents, never null
        Throws:
        IOException - in case of an I/O error
        Since:
        2.3
      • openInputStream

        public static InputStream openInputStream​(File file)
                                           throws IOException
        Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

        At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

        An exception is thrown if the file does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be read.

        Parameters:
        file - the file to open for input, must not be null
        Returns:
        a new InputStream for the specified file
        Throws:
        FileNotFoundException - if the file does not exist
        IOException - if the file object is a directory
        IOException - if the file cannot be read
        Since:
        1.3
      • writeStringToFile

        public static void writeStringToFile​(File file,
                                             String data,
                                             String encoding)
                                      throws IOException
        Writes a String to a file creating the file if it does not exist. NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
        Parameters:
        file - the file to write
        data - the content to write to the file
        encoding - the encoding to use, null means platform default
        Throws:
        IOException - in case of an I/O error
        UnsupportedEncodingException - if the encoding is not supported by the VM
      • writeStringToFile

        public static void writeStringToFile​(File file,
                                             String data,
                                             Charset encoding,
                                             boolean append)
                                      throws IOException
        Writes a String to a file creating the file if it does not exist.
        Parameters:
        file - the file to write
        data - the content to write to the file
        encoding - the encoding to use, null means platform default
        append - if true, then the String will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error
        Since:
        2.3
      • openOutputStream

        public static OutputStream openOutputStream​(File file,
                                                    boolean append)
                                             throws IOException
        Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

        At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

        The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.

        Parameters:
        file - the file to open for output, must not be null
        append - if true, then bytes will be added to the end of the file rather than overwriting
        Returns:
        a new OutputStream for the specified file
        Throws:
        IOException - if the file object is a directory
        IOException - if the file cannot be written to
        IOException - if a parent directory needs creating but that fails
        Since:
        2.1
      • getTempDirectory

        public static File getTempDirectory()
        Returns a File representing the system temporary directory.
        Returns:
        the system temporary directory.
        Since:
        2.0
      • deleteQuietly

        public static boolean deleteQuietly​(File file)
        Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.

        The difference between File.delete() and this method are:

        • A directory to be deleted does not have to be empty.
        • No exceptions are thrown when a file or directory cannot be deleted.
        Parameters:
        file - file or directory to delete, can be null
        Returns:
        true if the file or directory was deleted, otherwise false
        Since:
        1.4
      • writeByteArrayToFile

        public static void writeByteArrayToFile​(File file,
                                                byte[] data)
                                         throws IOException
        Writes a byte array to a file creating the file if it does not exist.

        NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.

        Parameters:
        file - the file to write to
        data - the content to write to the file
        Throws:
        IOException - in case of an I/O erroe
        Since:
        1.1
      • writeByteArrayToFile

        public static void writeByteArrayToFile​(File file,
                                                byte[] data,
                                                boolean append)
                                         throws IOException
        Writes a byte array to a file creating the file if it does not exist.
        Parameters:
        file - the file to write to
        data - the content to write to the file
        append - if true, then bytes will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error
        Since:
        2.1
      • writeByteArrayToFile

        public static void writeByteArrayToFile​(File file,
                                                byte[] data,
                                                int off,
                                                int len,
                                                boolean append)
                                         throws IOException
        Writes len bytes from the specified byte array starting at offset off to a file, creating the file if it does not exist.
        Parameters:
        file - the file to write to
        data - the content to write to the file
        off - the start offset in the data
        len - the number of bytes to write
        append - if true, then bytes will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error
        Since:
        2.5
      • readFileToByteArray

        public static byte[] readFileToByteArray​(File file)
                                          throws IOException
        Reads the contents of a file into a byte array. The file is always closed.
        Parameters:
        file - the file to read, must not be null
        Returns:
        the file contents, never null
        Throws:
        IOException - in case of an I/O error
        Since:
        1.1
      • openOutputStream

        public static OutputStream openOutputStream​(File file)
                                             throws IOException
        Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

        At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

        The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.

        Parameters:
        file - the file to open for output, must not be null
        Returns:
        a new OutputStream for the specified file
        Throws:
        IOException - if the file object is a directory
        IOException - if the file cannot be written to
        IOException - if a parent directory needs creating but that fails
        Since:
        1.3
      • readLines

        @Deprecated
        public static List<StringreadLines​(File file)
                                      throws IOException
        Deprecated.
        2.5 use readLines(File, Charset) instead
        Reads the contents of a file line by line to a List of Strings using the default encoding for the VM. The file is always closed.
        Parameters:
        file - the file to read, must not be null
        Returns:
        the list of Strings representing each line in the file, never null
        Throws:
        IOException - in case of an I/O error
        Since:
        1.3
      • readLines

        public static List<StringreadLines​(File file,
                                             Charset encoding)
                                      throws IOException
        Reads the contents of a file line by line to a List of Strings. The file is always closed.
        Parameters:
        file - the file to read, must not be null
        encoding - the encoding to use, null means platform default
        Returns:
        the list of Strings representing each line in the file, never null
        Throws:
        IOException - in case of an I/O error
        Since:
        2.3