Class FileUtil


  • public final class FileUtil
    extends Object
    Class which provides methods to get directory, filename, ... from a path to a file
    • Field Detail

      • FILE_SEPERATOR

        public static final String FILE_SEPERATOR
        Path-Seperator for the actual operating system
      • FILE_SEPERATOR_WINDOWS

        public static final String FILE_SEPERATOR_WINDOWS
        Path-Seperator from Windows
        See Also:
        Constant Field Values
      • FILE_SEPERATOR_CHAR_WINDOWS

        public static final char FILE_SEPERATOR_CHAR_WINDOWS
        Path-Seperator from Windows
        See Also:
        Constant Field Values
      • FILE_SEPERATOR_CHAR_LINUX

        public static final char FILE_SEPERATOR_CHAR_LINUX
        Path-Seperator from Linux
        See Also:
        Constant Field Values
      • WINDOWS_MAX_PATH_LENGTH

        public static final int WINDOWS_MAX_PATH_LENGTH
        Windows Max Path Length (Minus 1, because of Null Terminator)
        See Also:
        Constant Field Values
      • WINDOWS_MIN_FILENAME_LENGTH

        public static final int WINDOWS_MIN_FILENAME_LENGTH
        Windows Min Filename Length (8 for name, 1 for dot, 3 for extension)
        See Also:
        Constant Field Values
      • WINDOWS_MAX_FOLDER_PATH_LENGTH

        public static final int WINDOWS_MAX_FOLDER_PATH_LENGTH
        Windows Max Path Length for a folder only.
        See Also:
        Constant Field Values
      • PATH_WITHOUT_FILENAME_LENGTH_LIMIT

        public static final int PATH_WITHOUT_FILENAME_LENGTH_LIMIT
        Maximum length of a path without filename
        See Also:
        Constant Field Values
      • FILENAME_LENGTH_LIMIT

        public static final int FILENAME_LENGTH_LIMIT
        Maximum length of a filename
        See Also:
        Constant Field Values
      • FILENAME_ASCII_ONLY

        public static final int FILENAME_ASCII_ONLY
        Filter all non-ascii chars from paths and filenames
        See Also:
        Constant Field Values
      • FILENAME_ASCII_UMLAUT

        public static final int FILENAME_ASCII_UMLAUT
        Filter all non-ascii and non-umlaut chars from paths and filenames
        See Also:
        Constant Field Values
      • FILENAME_ALL

        public static final int FILENAME_ALL
        Filter only not allowed chars from paths and filenames
        See Also:
        Constant Field Values
    • Method Detail

      • getDirectory

        public static String getDirectory​(String file)
        Returns the folder
        Parameters:
        file - Path to the file
        Returns:
        Pfad zum Verzeichnis
      • getFilename

        public static String getFilename​(String file)
        Returns the filename
        Parameters:
        file - Path to the file
        Returns:
        Filename
      • getFileExtension

        public static String getFileExtension​(String file)
        Returns the extension of a file
        Parameters:
        file - Path to the file or filename
        Returns:
        Fileextension
      • getFilePrefix

        public static String getFilePrefix​(String file)
        Returns the filename without the extension
        Parameters:
        file - Path to the file or filename
        Returns:
        Filename without extension
      • getPathFromFile

        public static String getPathFromFile​(File file)
        Returns the path of a file
        Parameters:
        file - File
        Returns:
        Path
      • checkIsSameOrSubFolder

        public static boolean checkIsSameOrSubFolder​(String path,
                                                     String parentPath)
        Checks if path is a subfolder of parentPath It will also return true, if the path is not a direct subfolder. And if path and parentPath are the same, this method will also return true! This method does not check if the folders exist! Example: parentPath = C:\ path = C:\example -- this returns true Example: parentPath = C:\ path = C:\example\something\bla\blub -- this returns true
        Parameters:
        path - Path
        parentPath - Parent Path
        Returns:
        TRUE if it is the same folder or a subfolder
      • lTrim

        public static String lTrim​(String str)
        Trims the start of a path
        Parameters:
        str - Path
        Returns:
        Path
      • rTrim

        public static String rTrim​(String str)
        Trims the end of a path
        Parameters:
        str - Path
        Returns:
        Path
      • pathRTrim

        public static String pathRTrim​(String path)
        Trims a path
        Parameters:
        path - Path
        Returns:
        Path
      • getNumberedFilename

        public static String getNumberedFilename​(String filename,
                                                 int number)
        Returns a filename with "#" replaced by the given number If there are more # as the numbers length is than the # is replaced by zero. Example: The filename is "file###name" and the number is 2, then this method will return this: "file002name"
        Parameters:
        filename - Filename
        number - Number
        Returns:
        Filename
      • getZeroFilledNumber

        public static String getZeroFilledNumber​(int number,
                                                 int len)
        Returns the number with leading zeros Example: Number is 2 and length is 4: This method returns: 0002 If the length is smaller than the count of chars of the number the length is automatically increased
        Parameters:
        number - Number
        len - Length
        Returns:
        Number with leading zeros
      • reducePathLength

        public static String reducePathLength​(String folder)
        Method to reduce length of path (without filename), because paths in Windows Explorer can only be have 255 chars.
        Parameters:
        folder - Path without Filename
        Returns:
        Reduced Path
      • reducePathLength

        public static String reducePathLength​(String folder,
                                              int lengthLimit)
        Method to reduce length of path (without filename), because paths in Windows Explorer can only be have 255 chars.
        Parameters:
        folder - Path without Filename
        lengthLimit - Limit
        Returns:
        Reduced Path
      • reduceFilenameLength

        public static String reduceFilenameLength​(String filename)
        Method to reduce length of filename, because paths in Windows Explorer can only be have 255 chars.
        Parameters:
        filename - Filename
        Returns:
        Reduced Filename
      • reduceFilenameLength

        public static String reduceFilenameLength​(String filename,
                                                  int lengthLimit)
        Method to reduce length of filename, because paths in Windows Explorer can only be have 255 chars.
        Parameters:
        filename - Filename
        lengthLimit - Limit
        Returns:
        Reduced Filename
      • reduceTextLength

        public static String reduceTextLength​(String text,
                                              int limit)
        Method to reduce lenght of a String which is not a filename or a path.
        Parameters:
        text - Text
        limit - Limit
        Returns:
        Reduced Text
      • filterPath

        public static String filterPath​(String path,
                                        int mode)
        This method filters not allowed chars in paths (including filename if available)
        Parameters:
        path - Path
        mode - Mode
        Returns:
        Filtered path
      • filterFilename

        public static String filterFilename​(String filename,
                                            int mode)
        This method filters not allowed chars in filenames
        Parameters:
        filename - Filename
        mode - Mode
        Returns:
        Filtered Filename