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 path by this method uses the default name-separator to separate names in the path.
Syntax:
String toString()
Parameters: This method accepts nothing.
Return value: This method returns the string representation of this path.
Below programs illustrate toString() method:
Program 1:
// Java program to demonstrate // java.nio.file.Path.toAbsolute() method import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static void main(String[] args) throws IOException { // create object of Path Path path = Paths.get("C:\\Program Files\\" + "Java\\jre1.8.0_211"); // print path System.out.println("Path: " + path.toString()); } } |
Path: C:\Program Files\Java\jre1.8.0_211
Program 2:
// Java program to demonstrate // java.nio.file.Path.toString() method import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static void main(String[] args) throws IOException { // create object of Path Path path = Paths.get("C:\\Users\\" + "asingh.one\\Documents"); // print path System.out.println("Path: " + path.toString()); } } |
Path: C:\Users\asingh.one\Documents
References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#toString()
Recommended Posts:
- Duration toString() method in Java with Examples
- AbstractCollection toString() method in Java with Examples
- Calendar toString() Method in Java with Examples
- Modifiers toString() method in Java with Examples
- OptionalLong toString() method in Java with examples
- LinkedTransferQueue toString() method in Java with Examples
- AtomicInteger toString() method in Java with examples
- LocalTime toString() method in Java with Examples
- ShortBuffer toString() method in Java with Examples
- MessageDigest toString() method in Java with Examples
- CharArrayWriter toString() method in Java with examples
- AtomicLong toString() method in Java with examples
- AlgorithmParameters toString() method in Java with Examples
- DigestOutputStream.toString() method in Java with Examples
- BitSet toString() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



