Class FileUtilities


  • public class FileUtilities
    extends Object

    Various file utilities useful when dealing with bytes, bits and numbers

    Since:
    1.1.0
    Author:
    Andrea Antonello - www.hydrologis.com
    • Constructor Detail

      • FileUtilities

        public FileUtilities()
    • Method Detail

      • deleteFileOrDir

        public static boolean deleteFileOrDir​(File filehandle)
                                       throws IOException
        Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false.
        Parameters:
        filehandle - the file or folder to remove.
        Returns:
        true if all deletions were successful
        Throws:
        IOException
      • readFileToBytes

        public static byte[] readFileToBytes​(String filePath)
                                      throws IOException
        Read a file into a byte array.
        Parameters:
        filePath - the path to the file.
        Returns:
        the array of bytes.
        Throws:
        IOException
      • readInputStreamToString

        public static String readInputStreamToString​(InputStream inputStream)
                                              throws IOException
        Read from an inoutstream and convert the readed stuff to a String. Usefull for text files that are available as streams.
        Parameters:
        inputStream -
        Returns:
        the read string
        Throws:
        IOException
      • readFile

        public static String readFile​(String filePath)
                               throws IOException
        Read text from a file in one line.
        Parameters:
        filePath - the path to the file to read.
        Returns:
        the read string.
        Throws:
        IOException
      • readFile

        public static String readFile​(File file)
                               throws IOException
        Read text from a file in one line.
        Parameters:
        file - the file to read.
        Returns:
        the read string.
        Throws:
        IOException
      • readFileToLinesList

        public static List<String> readFileToLinesList​(String filePath)
                                                throws IOException
        Read text from a file to a list of lines.
        Parameters:
        filePath - the path to the file to read.
        Returns:
        the list of lines.
        Throws:
        IOException
      • readFileToLinesList

        public static List<String> readFileToLinesList​(File file)
                                                throws IOException
        Read text from a file to a list of lines.
        Parameters:
        file - the file to read.
        Returns:
        the list of lines.
        Throws:
        IOException
      • writeFile

        public static void writeFile​(String text,
                                     File file)
                              throws IOException
        Write text to a file in one line.
        Parameters:
        text - the text to write.
        file - the file to write to.
        Throws:
        IOException
      • writeFile

        public static void writeFile​(List<String> lines,
                                     String filePath)
                              throws IOException
        Write a list of lines to a file.
        Parameters:
        lines - the list of lines to write.
        filePath - the path to the file to write to.
        Throws:
        IOException
      • writeFile

        public static void writeFile​(List<String> lines,
                                     File file)
                              throws IOException
        Write a list of lines to a file.
        Parameters:
        lines - the list of lines to write.
        file - the file to write to.
        Throws:
        IOException
      • replaceBackSlashes

        public static String replaceBackSlashes​(String path)
      • replaceBackSlashesWithSlashes

        public static String replaceBackSlashesWithSlashes​(String string)
        Replaces backslashes with /.
        Parameters:
        string - the string to check.
        Returns:
        the string without backslashes.
      • getNameWithoutExtention

        public static String getNameWithoutExtention​(File file)
        Returns the name of the file without the extension.

        Note that if the file has no extension, the name is returned.

        Parameters:
        file - the file to trim.
        Returns:
        the name without extension.
      • substituteExtention

        public static File substituteExtention​(File file,
                                               String newExtention)
        Substitute the extention of a file.
        Parameters:
        file - the file.
        newExtention - the new extention (without the dot).
        Returns:
        the file with the new extention.
      • getSafeFileName

        public static String getSafeFileName​(String fileName)
        Makes a file name safe to be used.
        Parameters:
        fileName - the file name to "encode".
        Returns:
        the safe filename.
      • readFileToHashMap

        public static LinkedHashMap<String,​String> readFileToHashMap​(String filePath,
                                                                           String separator,
                                                                           boolean valueFirst)
                                                                    throws IOException
        Method to read a properties file into a LinkedHashMap.

        Empty lines are ignored, as well as lines that do not contain the separator.

        Parameters:
        filePath - the path to the file to read.
        separator - the separator or null. Defaults to '='.
        valueFirst - if true, the second part of the string is used as key.
        Returns:
        the read map.
        Throws:
        IOException
      • stringAsTmpFile

        public static File stringAsTmpFile​(String string)
                                    throws Exception
        "Converts" a string to a temporary file.

        Useful for those modules that want a file in input and one wants to use the parameters.

        Parameters:
        string - the string to write to the file.
        Returns:
        the created file.
        Throws:
        Exception
      • stringListAsTmpFile

        public static File stringListAsTmpFile​(List<String> list)
                                        throws Exception
        "Converts" a List of strings to a temporary file.

        Useful for those modules that want a file in input and one wants to use the parameters.

        Parameters:
        list - the list of strings to write to the file (one per row).
        Returns:
        the created file.
        Throws:
        Exception
      • getFilesListByExtention

        public static File[] getFilesListByExtention​(String folderPath,
                                                     String ext)
        Get the list of files in a folder by its extension.
        Parameters:
        folderPath - the folder path.
        ext - the extension without the dot.
        Returns:
        the list of files patching.
      • folderCheckMakeOrDie

        public static void folderCheckMakeOrDie​(String path)
                                         throws ModelsIOException
        Checks a path to be a folder. If it doesn't exist, it tries to create it.
        Throws:
        ModelsIOException - if the path is null or impossible to create.