Class Path

  • All Implemented Interfaces:
    Serializable

    @Public
    public class Path
    extends Object
    implements Serializable
    Names a file or directory in a FileSystem. Path strings use slash as the directory separator. A path string is absolute if it begins with a slash.

    Tailing slashes are removed from the path.

    Note: Path will no longer implement IOReadableWritable in future versions. Please use serializeToDataOutputView and deserializeFromDataInputView instead.

    See Also:
    Serialized Form
    • Constructor Detail

      • Path

        public Path()
        Constructs a new (empty) path object (used to reconstruct path object after RPC call).
      • Path

        public Path​(URI uri)
        Constructs a path object from a given URI.
        Parameters:
        uri - the URI to construct the path object from
      • Path

        public Path​(String parent,
                    String child)
        Resolve a child path against a parent path.
        Parameters:
        parent - the parent path
        child - the child path
      • Path

        public Path​(Path parent,
                    String child)
        Resolve a child path against a parent path.
        Parameters:
        parent - the parent path
        child - the child path
      • Path

        public Path​(String parent,
                    Path child)
        Resolve a child path against a parent path.
        Parameters:
        parent - the parent path
        child - the child path
      • Path

        public Path​(Path parent,
                    Path child)
        Resolve a child path against a parent path.
        Parameters:
        parent - the parent path
        child - the child path
      • Path

        public Path​(String pathString)
        Construct a path from a String. Path strings are URIs, but with unescaped elements and some additional normalization.
        Parameters:
        pathString - the string to construct a path from
      • Path

        public Path​(String scheme,
                    String authority,
                    String path)
        Construct a Path from a scheme, an authority and a path string.
        Parameters:
        scheme - the scheme string
        authority - the authority string
        path - the path string
    • Method Detail

      • toUri

        public URI toUri()
        Converts the path object to a URI.
        Returns:
        the URI object converted from the path object
      • getFileSystem

        public FileSystem getFileSystem()
                                 throws IOException
        Returns the FileSystem that owns this Path.
        Returns:
        the FileSystem that owns this Path
        Throws:
        IOException - thrown if the file system could not be retrieved
      • isAbsolute

        public boolean isAbsolute()
        Checks if the directory of this path is absolute.
        Returns:
        true if the directory of this path is absolute, false otherwise
      • getName

        public String getName()
        Returns the final component of this path, i.e., everything that follows the last separator.
        Returns:
        the final component of the path
      • getPath

        public String getPath()
        Return full path.
        Returns:
        full path
      • getParent

        public Path getParent()
        Returns the parent of a path, i.e., everything that precedes the last separator or null if at root.
        Returns:
        the parent of a path or null if at root.
      • suffix

        public Path suffix​(String suffix)
        Adds a suffix to the final name in the path.
        Parameters:
        suffix - The suffix to be added
        Returns:
        the new path including the suffix
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • compareTo

        public int compareTo​(Object o)
      • depth

        public int depth()
        Returns the number of elements in this path.
        Returns:
        the number of elements in this path
      • makeQualified

        public Path makeQualified​(FileSystem fs)
        Returns a qualified path object.
        Parameters:
        fs - the FileSystem that should be used to obtain the current working directory
        Returns:
        the qualified path object
      • hasWindowsDrive

        public boolean hasWindowsDrive()
        Checks if the provided path string contains a windows drive letter.
        Returns:
        True, if the path string contains a windows drive letter, false otherwise.
      • fromLocalFile

        public static Path fromLocalFile​(File file)
        Creates a path for the given local file.

        This method is useful to make sure the path creation for local files works seamlessly across different operating systems. Especially Windows has slightly different rules for slashes between schema and a local file path, making it sometimes tricky to produce cross-platform URIs for local files.

        Parameters:
        file - The file that the path should represent.
        Returns:
        A path representing the local file URI of the given file.
      • deserializeFromDataInputView

        @Nullable
        public static Path deserializeFromDataInputView​(DataInputView in)
                                                 throws IOException
        Deserialize the Path from DataInputView.
        Parameters:
        in - the data input view.
        Returns:
        the path
        Throws:
        IOException - if an error happened.
      • serializeToDataOutputView

        public static void serializeToDataOutputView​(Path path,
                                                     DataOutputView out)
                                              throws IOException
        Serialize the path to DataInputView.
        Parameters:
        path - the file path.
        out - the data out put view.
        Throws:
        IOException - if an error happened.