Class FileUtils


  • public class FileUtils
    extends Object
    FileUtils provides some simple helper methods for working with files. It follows the convention of wrapping all checked exceptions as runtimes, so code using these methods is free of try-catch blocks but does not expect to recover from errors.
    • Method Detail

      • readFileAsBytes

        public static byte[] readFileAsBytes​(String filename)
        Reads a text file as a string.
        Parameters:
        filename - The name of the file.
        Returns:
        The contents of the file.
      • readFileAsString

        public static String readFileAsString​(String filename)
        Reads a text file as a string.
        Parameters:
        filename - The name of the file.
        Returns:
        The contents of the file.
      • readFileAsString

        public static String readFileAsString​(File file)
        Reads a text file as a string.
        Parameters:
        file - The file.
        Returns:
        The contents of the file.
      • openFileOrDefaultResource

        public static InputStream openFileOrDefaultResource​(String filename,
                                                            String defaultResource,
                                                            ClassLoader cl)
        Either opens the specified filename as an input stream or either the filesystem or classpath, or uses the default resource loaded using the specified class loader, if opening the file fails or no file name is specified.
        Parameters:
        filename - The name of the file to open.
        defaultResource - The name of the default resource on the classpath if the file cannot be opened.
        cl - The classloader to load the default resource with.
        Returns:
        An input stream for the file or resource, or null if one could not be opened.
      • copy

        public static void copy​(File src,
                                File dst)
        Copies the specified source file to the specified destintaion file. If the destinationst file does not exist, it is created.
        Parameters:
        src - The source file name.
        dst - The destination file name.
      • copyCheckedEx

        public static void copyCheckedEx​(File src,
                                         File dst)
                                  throws IOException
        Copies the specified source file to the specified destination file. If the destination file does not exist, it is created.
        Parameters:
        src - The source file name.
        dst - The destination file name.
        Throws:
        IOException - if there is an issue copying the file
      • copy

        public static void copy​(InputStream in,
                                File dst)
                         throws IOException
        Copies the specified InputStream to the specified destination file. If the destination file does not exist, it is created.
        Parameters:
        in - The InputStream
        dst - The destination file name.
        Throws:
        IOException - if there is an issue copying the stream
      • deleteFile

        public static boolean deleteFile​(String filePath)
      • deleteDirectory

        public static boolean deleteDirectory​(String directoryPath)
      • delete

        public static boolean delete​(File file,
                                     boolean recursive)
        Delete a given file/directory, A directory will always require the recursive flag to be set. if a directory is specified and recursive set then delete the whole tree
        Parameters:
        file - the File object to start at
        recursive - boolean to recurse if a directory is specified.
        Returns:
        true if and only if the file or directory is successfully deleted; false otherwise
      • searchFile

        public static List<String> searchFile​(File file,
                                              String search)
                                       throws IOException
        Checks the specified file for instances of the search string.
        Parameters:
        file - the file to search
        search - the search String
        Returns:
        the list of matching entries
        Throws:
        IOException - if there is an issue searching the file