Class FileUtils


  • public class FileUtils
    extends Object
    Utility class for handling files.
    Author:
    Michele Mostarda (mostarda@fbk.eu)
    • Method Detail

      • mv

        public static File mv​(File target,
                              File dest)
        Moves a target file to a new dest location.
        Parameters:
        target - file to be moved.
        dest - dest dir.
        Returns:
        destination file.
      • cp

        public static void cp​(InputStream is,
                              File dest)
        Copies the content of the input stream within the given dest file. The dest file must not exist.
        Parameters:
        is - InputStream to copy
        dest - detination to copy it to.
      • dumpContent

        public static void dumpContent​(File f,
                                       String content)
                                throws IOException
        Dumps the given string within a file.
        Parameters:
        f - file target.
        content - content to be dumped.
        Throws:
        IOException - if there is an error dumping the content
      • dumpContent

        public static void dumpContent​(File f,
                                       Throwable t)
                                throws IOException
        Dumps the stack trace of the given exception into the specified file.
        Parameters:
        f - file to generate dump.
        t - exception to be dumped.
        Throws:
        IOException - if there is an error dumping the content
      • readResourceContent

        public static String readResourceContent​(Class clazz,
                                                 String resource)
                                          throws IOException
        Reads a resource file and returns the content as a string.
        Parameters:
        clazz - the class to use load the resource.
        resource - the resource to be load.
        Returns:
        the string representing the file content.
        Throws:
        IOException - if there is an error loading the resource
      • readResourceContent

        public static String readResourceContent​(String resource)
                                          throws IOException
        Reads a resource file and returns the content as a string.
        Parameters:
        resource - the resource to be load.
        Returns:
        the string representing the file content.
        Throws:
        IOException - if there is an error loading the resource
      • readFileContent

        public static String readFileContent​(File f)
                                      throws IOException
        Returns the content of a file a single string.
        Parameters:
        f - the file to read.
        Returns:
        the content of file.
        Throws:
        IOException - if an error occurs while locating or accessing the file.
      • readFileLines

        public static String[] readFileLines​(File f)
                                      throws IOException
        Returns all the lines of a file.
        Parameters:
        f - the file to read.
        Returns:
        a not null array with not null line strings.
        Throws:
        IOException - if an error occurs while locating or accessing the file.
      • listFilesRecursively

        public static File[] listFilesRecursively​(File dir,
                                                  FilenameFilter filenameFilter)
        Lists the content of a dir applying the specified filter.
        Parameters:
        dir - directory root.
        filenameFilter - filter to be applied.
        Returns:
        list of matching files.