Class SmartFile


  • public class SmartFile
    extends Object
    A class for sanitizing Files. Note that the main reason for this class is that on non-Windows, getCanonicalXXX and getAbsoluteXXX might point at different files. If the file is a soft link then the Canonical will be the file that is linked to. The Absolute will be the link file itself. This method will give you the benefits of Canonical -- but will always point at the link file itself. Windows is horribly complex compared to "everything else". Windows does not have the symbolic link issue -- so use getCanonicalXXX to do the work on Windows. Windows will return paths with all forward slashes -- no backward slashes unless it is the special Windows network address that starts with "\\"

    I.e. It is just like getAbsoluteXXX -- but it removes all relative path elements from the path.

    Author:
    bnevins
    • Method Detail

      • sanitize

        public static File sanitize​(File f)
        Sanitize a File object -- remove all relative path portions, i.e. dots e.g. "/xxx/yyy/././././../yyy" --> /xxx/yyy on UNIX, perhaps C:/xxx/yyy on Windows
        Parameters:
        f - The file to sanitize
        Returns:
        THe sanitized File
      • sanitize

        public static String sanitize​(String filename)
        Sanitize a path -- remove all relative path portions, i.e. dots e.g. "/xxx/yyy/././././../yyy" --> /xxx/yyy on UNIX, perhaps C:/xxx/yyy on Windows Note that the main reason for this class is that on non-Windows, getCanonicalXXX and getAbsoluteXXX might point at different files. If the file is a soft link then the Canonical will be the file that is linked to. The Absolute will be the link file itself. This method will give you the benefits of Canonical -- but will always point at the link file path itself.
        Parameters:
        filename - The path to sanitize
        Returns:
        The sanitized path
      • sanitizePaths

        public static String sanitizePaths​(String pathsString)
        Sanitize a "Classpath-like" list of Paths.
        Parameters:
        pathsString - A string of paths, each separated by File.pathSeparator
        Returns:
        The sanitized paths