Class FileUtils


  • public class FileUtils
    extends Object
    This class contains utility methods related to the file system.
    • Constructor Detail

      • FileUtils

        public FileUtils()
    • Method Detail

      • mkdir

        public static void mkdir​(File dir)
                          throws IOException
        Creates the specified directory and all non-existing parent directories.
        Parameters:
        dir - the directory
        Throws:
        IOException - if the directory can't be created
      • truncate

        public static void truncate​(File file,
                                    long len)
                             throws IOException
        Truncates the specified file so it won't be longer than the specified length. This method will copy the file to a temporary file and then copy back at most "len" bytes.
        Parameters:
        file - the file
        len - the length
        Throws:
        IOException - if a reading/writing error occurs
      • createTempFile

        public static File createTempFile​(File dir,
                                          String prefix,
                                          String ext)
                                   throws IOException
        Creates a temporary file in the specified directory. The file name will be formatted as "prefix.number.ext", where number is a sequence of eight hexadecimal digits. This method will use a file name that doesn't already exist and it atomically creates an empty file with that name.
        Parameters:
        dir - the directory
        prefix - the prefix
        ext - the extension
        Returns:
        the file
        Throws:
        IOException - if a writing error occurs
      • copyFile

        public static void copyFile​(File source,
                                    File dest)
                             throws IOException
        Copies the specified source file to the specified destination file.
        Parameters:
        source - the source file
        dest - the destination file
        Throws:
        IOException - if a read/write error occurs
      • copyFile

        public static void copyFile​(File source,
                                    File dest,
                                    long start,
                                    long len)
                             throws IOException
        Copies bytes from the specified source file to the specified destination file.
        Parameters:
        source - the source file
        dest - the destination file
        start - the index of the first byte to copy
        len - the number of bytes to copy
        Throws:
        IOException - if a read/write error occurs
      • deleteTree

        public static void deleteTree​(File dir)
                               throws IOException
        Deletes the specified directory including all its contents. If dir is a file, it will just delete that file.
        Parameters:
        dir - the directory or file
        Throws:
        IOException - if a file or directory can't be deleted
      • clearDirectory

        public static void clearDirectory​(File dir)
                                   throws IOException
        Deletes the content of the specified directory. If the directory doesn't exist, this method has no effect.
        Parameters:
        dir - the directory
        Throws:
        IOException - if the specified path is not a directory or if a file or directory can't be deleted
      • readFileBytes

        public static byte[] readFileBytes​(File file)
                                    throws IOException
        Reads the content of the specified file as a byte array.
        Parameters:
        file - the file
        Returns:
        the content
        Throws:
        IOException - if a reading error occurs
      • readFileBytes

        public static byte[] readFileBytes​(URL url)
                                    throws IOException
        Reads the content of the specified file as a byte array.
        Parameters:
        url - the URL for the file
        Returns:
        the content
        Throws:
        IOException - if a reading error occurs
      • readFileBytes

        public static byte[] readFileBytes​(InputStream input)
                                    throws IOException
        Reads the content of the specified file as a byte array.
        Parameters:
        input - the input stream from the file
        Returns:
        the content
        Throws:
        IOException - if a reading error occurs
      • readFileString

        public static String readFileString​(File file)
                                     throws IOException
        Reads the content of the specified file as a string. It assumes character encoding UTF-8.
        Parameters:
        file - the file
        Returns:
        the content
        Throws:
        IOException - if a reading error occurs
      • readFileString

        public static String readFileString​(URL url)
                                     throws IOException
        Reads the content of the specified file as a string. It assumes character encoding UTF-8.
        Parameters:
        url - the URL for the file
        Returns:
        the content
        Throws:
        IOException - if a reading error occurs
      • readFileString

        public static String readFileString​(InputStream input)
                                     throws IOException
        Reads the content of the specified file as a string. It assumes character encoding UTF-8.
        Parameters:
        input - the input stream from the file
        Returns:
        the content
        Throws:
        IOException - if a reading error occurs
      • readFileString

        public static String readFileString​(Reader reader)
                                     throws IOException
        Reads the content of the specified file as a string.
        Parameters:
        reader - the reader from the file
        Returns:
        the content
        Throws:
        IOException - if a reading error occurs
      • writeFileString

        public static void writeFileString​(File file,
                                           String content)
                                    throws IOException
        Writes a string to a file with encoding UTF-8.
        Parameters:
        file - the file
        content - the content string to write
        Throws:
        IOException - if a writing error occurs
      • writeFileString

        public static void writeFileString​(OutputStream output,
                                           String content)
                                    throws IOException
        Writes a string to an output stream with encoding UTF-8.
        Parameters:
        output - the output stream
        content - the content string to write
        Throws:
        IOException - if a writing error occurs