The Wayback Machine - https://web.archive.org/web/20240912165426/https://www.geeksforgeeks.org/path-subpath-method-in-java-with-examples/
Open In App

Path subpath() method in Java with Examples

Last Updated : 20 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The subpath(int beginIndex, int endIndex) method of java.nio.file.Path used to return a relative Path that is a subsequence of the name elements of this path. We will pass begin and end indexes to construct a subpath. The beginIndex and endIndex parameters specify the subsequence of name elements. The name element closest to the root in the directory hierarchy is index 0 and name element that is farthest from the root has index count-1. The returned subpath object has the name elements that begin at beginIndex and extend to the element at index endIndex-1. 

Syntax:

Path subpath(int beginIndex,
             int endIndex)

Parameters: This method accepts a two parameters:

  • beginIndex which is the index of the first element, inclusive and
  • endIndex which is the index of the last element, exclusive.

Return value: This method returns a new Path object that is a subsequence of the name elements in this Path. 

Exception: This method throws IllegalArgumentException if beginIndex is negative, or greater than or equal to the number of elements. If endIndex is less than or equal to beginIndex, or larger than the number of elements. 

Below programs illustrate subpath() method: 

Program 1: 

Java




// Java program to demonstrate
// java.nio.file.Path.subpath() method
 
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
    public static void main(String[] args)
    {
 
        // create an object of Path
        Path path
            = Paths.get("D:\\eclipse\\p2"
                        + "\\org\\eclipse\\equinox\\p2\\core"
                        + "\\cache\\binary");
 
        // call subPath() to create a subPath which
        // begin at index 1 and ends at index 5
        Path subPath = path.subpath(1, 5);
 
        // print result
        System.out.println("Subpath: "
                           + subPath);
    }
}


Output:

Program 2: 

Java




// Java program to demonstrate
// java.nio.file.Path.subpath() method
 
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
    public static void main(String[] args)
    {
 
        // create an object of Path
        Path path
            = Paths.get("D:\\Workspace"
                        + "\\nEclipseWork"
                        + "\\GFG\\bin\\defaultpackage");
 
        System.out.println("Original Path:"
                           + path);
 
        // call subPath() to create a subPath which
        // begin at index 0 and ends at index 2
        Path subPath = path.subpath(0, 2);
 
        // print result
        System.out.println("Subpath: "
                           + subPath);
    }
}


Output:

References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#subpath(int, int)



Similar Reads

Path getFileName() method in Java with Examples
The Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types one is
2 min read
Path getNameCount() method in Java with Examples
The Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types one is
2 min read
Path compareTo() method in Java with Examples
The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types on
3 min read
Path getParent() method in Java with Examples
The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types on
2 min read
Path equals() method in Java with Examples
The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types on
2 min read
Path hashCode() method in Java with Examples
The Java Path interface was added to Java NIO in Java 7. hashCode() method of java.nio.file.Path used to return a hash code for this path after computing hashcode. The hashcode is always the same if the object doesn’t change. Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashi
2 min read
Path isAbsolute() method in Java with Examples
The Java Path interface was added to Java NIO in Java 7. isAbsolute() method of java.nio.file.Path used to check this path is absolute or not. This method return true if, and only if, this path is absolute. An absolute path is a path that doesn't need to be combined with other path information in order to locate a file. Syntax: boolean isAbsolute()
2 min read
Path getRoot() method in Java with Examples
The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types on
2 min read
Path toString() method in Java with Examples
The Java Path interface was added to Java NIO in Java 7. toString() method of java.nio.file.Path used to return the string representation of this path. If this path was created by converting a path string using the getPath method then the path string returned by this method may differ from the original String used to create the path. The returned p
2 min read
Path toAbsolutePath() method in Java with Examples
The Java Path interface was added to Java NIO in Java 7. toAbsolutePath() method of java.nio.file.Path used to return a Path object representing the absolute path of this path object. If the path is already absolute then the method returns path else this method resolves this path in an implementation-dependent manner by resolving the path against a
2 min read
Path getName(int) method in Java with Examples
The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types on
2 min read
Path getFileSystem() method in Java with Examples
The Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directory.path of an entity could be of two types one is
2 min read
Path toUri() method in Java with Examples
The toUri() method of java.nio.file.Path interface used to return a URI to represent this path. This method converts this path into an absolute URI with a scheme equal to the URI scheme that identifies the provider. The exact form of the scheme-specific part is highly provider dependent. In the scenario of the default provider, the URI is hierarchi
2 min read
Path toFile() method in Java with Examples
The toFile() method of java.nio.file.Path interface used to return a java.io.File object representing this path object. if this Path is associated with the default provider, then this method returns a java.io.File object constructed with the String representation of this path. If this path was created by invoking the java.io.File toPath method then
2 min read
Path resolve() method in Java with Examples
resolve() method of java.nio.file.Path used to resolve the given path against this path. There are two types of resolve() methods. resolve(String other) method of java.nio.file.Path used to converts a given path string to a Path and resolves it against this Path in the exact same manner as specified by the resolve method. For example, If the name s
3 min read
Path resolveSibling() method in Java with Examples
resolveSibling() method of java.nio.file.Path used to resolve the given path against this path's parent path.. There are two types of resolveSibling() methods. resolveSibling(Path other) method of java.nio.file.Path used to resolve the given path as parameter against this path's parent path. This is very useful where a file name needs to be replace
3 min read
Path endsWith() method in Java with Examples
endswith() method of java.nio.file.Path used to check if this path object ends with the given path or string which we passed as parameter. There are two types of endsWith() methods. endsWith(String other) method of java.nio.file.Path used to check if this path ends with a Path, constructed by converting the given path string which we passed as a pa
3 min read
Path startsWith() method in Java with Examples
startsWith() method of java.nio.file.Path used to check if this path object starts with the given path or string which we passed as parameter. There are two types of startsWith() methods. startsWith(String other) method of java.nio.file.Path used to check if this path starts with a Path, constructed by converting the given path string which we pass
3 min read
Path relativize() method in Java with Examples
The relativize(Path other) method of java.nio.file.Path used to create a relative path between this path and a given path as a parameter. Relativization is the inverse of resolution. This method creates a relative path that when resolved against this path object, yields a path that helps us to locate the same file as the given path. For example, if
2 min read
Path iterator() method in Java with Examples
The iterator() method of java.nio.file.Path used to returns an iterator of the name elements which construct this path. The first element of this iterator contains the name element that is closest to the root in the directory hierarchy, the second element is the next closest, and so on. The last element of this iterator is the name of the file or d
2 min read
Path normalize() method in Java with Examples
The normalize() method of java.nio.file.Path used to return a path from current path in which all redundant name elements are eliminated. The precise definition of this method is implementation dependent and it derives a path that does not contain redundant name elements. In many file systems, the "." and ".." are special names indicating the curre
2 min read
Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition che
3 min read
Java lang.Long.lowestOneBit() method in Java with Examples
java.lang.Long.lowestOneBit() is a built-in method in Java which first convert the number to Binary, then it looks for first set bit present at the lowest position then it reset rest of the bits and then returns the value. In simple language, if the binary expression of a number contains a minimum of a single set bit, it returns 2^(first set bit po
3 min read
Java lang.Long.numberOfTrailingZeros() method in Java with Examples
java.lang.Long.numberOfTrailingZeros() is a built-in function in Java which returns the number of trailing zero bits to the right of the lowest order set bit. In simple terms, it returns the (position-1) where position refers to the first set bit from the right. If the number does not contain any set bit(in other words, if the number is zero), it r
3 min read
Java lang.Long.numberOfLeadingZeros() method in Java with Examples
java.lang.Long.numberOfLeadingZeros() is a built-in function in Java which returns the number of leading zero bits to the left of the highest order set bit. In simple terms, it returns the (64-position) where position refers to the highest order set bit from the right. If the number does not contain any set bit(in other words, if the number is zero
3 min read
Java lang.Long.highestOneBit() method in Java with Examples
java.lang.Long.highestOneBit() is a built-in method in Java which first convert the number to Binary, then it looks for the first set bit from the left, then it reset rest of the bits and then returns the value. In simple language, if the binary expression of a number contains a minimum of a single set bit, it returns 2^(last set bit position from
3 min read
Java lang.Long.byteValue() method in Java with Examples
java.lang.Long.byteValue() is a built-in function in Java that returns the value of this Long as a byte. Syntax: public byte byteValue() Parameters: The function does not accept any parameter. Return : This method returns the numeric value represented by this object after conversion to byte type. Examples: Input : 12 Output : 12 Input : 1023 Output
3 min read
Java lang.Long.reverse() method in Java with Examples
java.lang.Long.reverse() is a built-in function in Java which returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified long value. Syntax: public static long reverse(long num) Parameter : num - the number passed Returns : the value obtained by reversing the order of the bits in the
2 min read
Java lang.Long.builtcount() method in Java with Examples
java.lang.Long.bitCount() is a built in function in Java that returns the number of set bits in a binary representation of a number. It accepts a single mandatory parameter number whose number of set bits is returned. Syntax: public static long bitCount(long num) Parameters: num - the number passed Returns: the number of set bits in the binary repr
3 min read
Java Clock tickMinutes() method in Java with Examples
java.time.Clock.tickMinutes(ZoneId zone) method is a static method of Clock class that returns the current instant ticking in whole minutes using the best available system clock and the time-zone of that instant is same as the time-zone passed as a parameter. Nanosecond and second fields of the clock are set to zero to round the instant in the whol
3 min read
Practice Tags :