Class Path


  • public class Path
    extends Object
    This class is used represent the path to a Directory or File item. Some examples:
    Path examples
    /The root diretory
    /filenameA file in the root diretory
    /directoryA directory in root diretory
    /directory/filenameA file in a directory
    /directory/subdirectoryA subdirectory
    /directory/subdirectory/filenameA file in a subdirectory
    ~The currently logged in user's home directory
    ~/filenameA file in the current user's home directory
    ~/directoryA directory in the current user's home directory
    ~loginThe home directory of the user with the specified login
    ~login/filenameA file in the home directory of the user with the specified login
    ~login/directoryA directory in the home directory of the user with the specified login
    Version:
    2.0
    Author:
    enell, nicklas
    Last modified
    $Date: 2015-04-21 09:59:42 +0200 (ti, 21 apr 2015) $
    • Field Detail

      • INVALID_CHARACTERS

        public static final String INVALID_CHARACTERS
        These characters are not valid within a file or directory name: "~\\/:;*?<>|\""
        See Also:
        Constant Field Values
      • invalid

        private static final Pattern invalid
        A regexp checking for invalid characters.
      • type

        private Path.Type type
        The type of the path.
      • userlogin

        private String userlogin
        The userlogin part of the path.
      • directories

        private String[] directories
        The directory part of the path.
      • filename

        private String filename
        The filename part of the path.
    • Constructor Detail

      • Path

        Path​(String userlogin,
             String[] directories,
             String filename)
        Create a new Path object.
        Parameters:
        userlogin - The userlogin part of the path
        directories - The directories in the path
        filename - The filename part of the path
      • Path

        Path​(Path directoryPath,
             String filename)
        Create a new Path object.
        Parameters:
        directoryPath - The directory path
        filename - The filename part of the path
    • Method Detail

      • isValidName

        public static boolean isValidName​(String name)
        Check if a name is valid as a partial path name, ie. it must not contain any of the characters in INVALID_CHARACTERS.
        Parameters:
        name - The string to be tested.
        Returns:
        TRUE if this name is valid.
      • makeSafeFilename

        public static String makeSafeFilename​(String originalName,
                                              String replaceWith)
        Convert a string to a name that is safe to use as a filename. It will replace all characters mathing any of the INVALID_CHARACTERS charactes with the specified string.
        Parameters:
        originalName - The original name
        replaceWith - The string to replace all unsafe characters with
        Since:
        2.2
      • getParent

        public Path getParent()
        Get the parent path.
        Returns:
        The path to the parent directory, or null if this is the root directory
      • getType

        public Path.Type getType()
        Get the type of the path.
        Returns:
        The Path.Type of the Path
      • getUserLogin

        public String getUserLogin()
        Get the userlogin part of the path. An empty string represents the currently logged in user. Null is returned if the path doesn't contain a user login.
        Returns:
        The login of the user
      • getDirectoryCount

        public int getDirectoryCount()
        Get the number of directories on the path.
        Returns:
        The number of directories
      • getDirectory

        public String getDirectory​(int i)
        Get the name of the i:th directory on the path, starting with 0.
        Parameters:
        i - Should be between 0 and getDirectoryCount-1
        Returns:
        The name of the directory
      • getFilename

        public String getFilename()
        Get the filename part of the path.
        Returns:
        The name of the file
      • toString

        public String toString()
        Get the string representation of the path.
        Overrides:
        toString in class Object
        Returns:
        The string representation of this path.
      • toURLString

        public String toURLString​(String enc)
                           throws UnsupportedEncodingException
        Get this Path's URL-string encoded with a given character encoding.
        Parameters:
        enc - The encoding to use. UTF-8 is used is this is null.
        Returns:
        The encoded URL-string.
        Throws:
        UnsupportedEncodingException - If the give encoding is not supported
        Since:
        2.11.1