Class FileUtil


  • public class FileUtil
    extends Object
    Util class to perform file system operations. All file operations wil be removed in the future in profit of the new Files class in the JDK.
    • Method Detail

      • getMimeType

        @NonNull
        public static String getMimeType​(String fileName)
        Detects the mime-type of the specified file.

        The mime-type is first extracted from its content. If the detection fails or if the file cannot be located by its specified name, then the mime-type is detected from the file extension.

        Parameters:
        fileName - the name of the file with its path.
        Returns:
        the mime-type of the specified file.
      • getAttachmentContext

        public static String[] getAttachmentContext​(String context)
        Create the array of strings this array represents the repertories where the files must be stored.
      • readFileToString

        public static String readFileToString​(File file)
                                       throws IOException
        Read the content of a file as text (the text is supposed to be in the UTF-8 charset). Instead of using this method, prefer to use the following Java > 7 statement:
        new String(Files.readAllBytes(file.toPath()), Charset.defaultCharset());
        Parameters:
        file - the file to read.
        Returns:
        the file content as a String.
        Throws:
        IOException - if an error occurs while reading the file.
      • isWindows

        public static boolean isWindows()
        Indicates if the OS is from the Microsoft Windows familly
        Returns:
        true if the OS is from the Microsoft Windows familly - false otherwise.
      • isSpinfireDocument

        public static boolean isSpinfireDocument​(String filename)
        If 3D document.
        Parameters:
        filename - the name of the file.
        Returns:
        true or false
      • isArchive

        public static boolean isArchive​(String filename)
        Indicates if the current file is of type archive.
        Parameters:
        filename - the name of the file.
        Returns:
        true is the file s of type archive - false otherwise.
      • isImage

        public static boolean isImage​(String filename)
        Indicates if the current file is of type image.
        Parameters:
        filename - the name of the file.
        Returns:
        true is the file is of type image - false otherwise.
      • isMail

        public static boolean isMail​(String filename)
        Indicates if the current file is of type mail.
        Parameters:
        filename - the name of the file.
        Returns:
        true is the file is of type mail - false otherwise.
      • isPdf

        public static boolean isPdf​(String filename)
        Indicates if the current file is of type PDF.
        Parameters:
        filename - the name of the file.
        Returns:
        true is the file s of type archive - false otherwise.
      • isOpenOfficeCompatible

        public static boolean isOpenOfficeCompatible​(String filename)
      • assertPathNotRelative

        public static void assertPathNotRelative​(String path)
                                          throws RelativeFileAccessException
        Asserts that the path doesn't contain relative navigation between paths.
        Parameters:
        path - the path to check
        Throws:
        RelativeFileAccessException - when a relative path is detected.
      • forceDeletion

        public static void forceDeletion​(File fileToDelete)
                                  throws IOException
        Forces the deletion of the specified file. If the write property of the file to delete isn't set, this property is then set before deleting.
        Parameters:
        fileToDelete - file to delete.
        Throws:
        IOException - if the deletion failed or if the file doesn't exist.
      • moveFile

        public static void moveFile​(File source,
                                    File destination)
                             throws IOException
        Moves the specified source file to the specified destination. If the destination exists, it is then replaced by the source; if the destination is a directory, then it is deleted with all of its contain.
        Parameters:
        source - the file to move.
        destination - the destination file of the move.
        Throws:
        IOException - if the source or the destination is invalid or if an error occurs while moving the file.
      • copyFile

        public static void copyFile​(File source,
                                    File destination)
                             throws IOException
        Copies the specified source file to the specified destination. If the destination exists, it is then replaced by the source. If the destination can be overwritten, its write property is set before the copy.
        Parameters:
        source - the file to copy.
        destination - the destination file of the move.
        Throws:
        IOException - if the source or the destination is invalid or if an error occurs while copying the file.
      • getFilename

        public static String getFilename​(String fileName)
      • convertPathToServerOS

        public static String convertPathToServerOS​(String undeterminedOsPath)
        Convert a path to the current OS path format.
        Parameters:
        undeterminedOsPath - a path
        Returns:
        server OS pah.
      • deleteEmptyDir

        public static boolean deleteEmptyDir​(@Nonnull
                                             File directory)
      • moveAllFilesAtRootFolder

        public static File[] moveAllFilesAtRootFolder​(File rootFolder)
                                               throws IOException
        Moves all files from sub folders to the given root folder and deletes after all the sub folders.
        Parameters:
        rootFolder - the root folder from which the sub folders are retrieved and into which the files will be moved if any.
        Returns:
        an array of File that represents the found sub folders. The returned array is never null.
        Throws:
        IOException - if an IO error occurs
      • moveAllFilesAtRootFolder

        public static File[] moveAllFilesAtRootFolder​(File rootFolder,
                                                      boolean deleteFolders)
                                               throws IOException
        Moves all files from sub folders to the given root folder.
        Parameters:
        rootFolder - the root folder from which the sub folders are retrieved and into which the files will be moved if any.
        deleteFolders - true if the sub folders must be deleted.
        Returns:
        an array of File that represents the found sub folders. The returned array is never null.
        Throws:
        IOException - if an IO error occurs
      • validateFilename

        public static void validateFilename​(String fileName,
                                            String intendedDir)
                                     throws IOException
        Validate that fileName given in parameter is inside extraction target directory (intendedDir)
        Parameters:
        fileName - the file name to extract
        intendedDir - the extraction target directory
        Throws:
        IOException - if fileName is outside extraction target directory
      • verifyTaintedData

        public static String verifyTaintedData​(String value)
        Verifies if the given data is a tainted one.
        Parameters:
        value - a string data representing a path.
        Returns:
        the given data.