Class FileUtils


  • public class FileUtils
    extends Object
    Lightweight File utilities
    Author:
    markr
    • Constructor Detail

      • FileUtils

        public FileUtils()
    • Method Detail

      • getExtension

        public static String getExtension​(File file)
        Collect the extension of a file. In case the file is hidden (starts with "." it does not count as extension)
        Parameters:
        file - the file to get it from
        Returns:
        the extension, if it does not exist return empty string
      • getFilesWithExtensionFromDir

        public static File[] getFilesWithExtensionFromDir​(String pathToDir,
                                                          String fileExtension)
        Collect all files from a directory with the given extension
        Parameters:
        pathToDir - path to dir
        fileExtension - the file extension, e.g. ".xml"
        Returns:
        the list of files that match this extension in the dir
      • getFileNameWithoutExtension

        public static String getFileNameWithoutExtension​(String fileName)
        Extract the file name without the extension, i.e. remove the last part of the string after the last "." encountered
        Parameters:
        fileName - to use
        Returns:
        fileName without extension
      • callForEachFileIn

        public static void callForEachFileIn​(String pathToDir,
                                             Consumer<File> callBack)
        Call the callback for each file in the directory provided
        Parameters:
        pathToDir - to check
        callBack - to use no each file found in dir
      • create

        public static File create​(URL url)
        Create from URL given it is a local file
        Parameters:
        url - to use
        Returns:
        file
      • deleteDirectory

        public static boolean deleteDirectory​(File directoryToDelete)
        Delete a directory by providing a file that represents a directory. In which case we recursively delete all files in the directory and then the directory itself.
        Parameters:
        directoryToDelete - the directory to delete
        Returns:
        success of deletion
      • parseFileContentAsString

        public static String parseFileContentAsString​(String filePath,
                                                      String charSetEncoding)
                                               throws IOException
        Construct a string based on the file contents
        Parameters:
        filePath - to use
        charSetEncoding - to apply
        Returns:
        created string representation
        Throws:
        IOException - thrown if error
      • parseUtf8FileContentAsString

        public static String parseUtf8FileContentAsString​(String filePath)
                                                   throws IOException
        Construct a string based on the file contents in UTF8 character encouding
        Parameters:
        filePath - to use
        Returns:
        created string representation
        Throws:
        IOException - thrown if error