Path toString() method in Java with Examples
Last Updated :
14 Apr, 2023
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
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
{
Path path
= Paths.get("C:\\Program Files\\"
+ "Java\\jre1. 8 .0_211");
System.out.println("Path: "
+ path.toString());
}
}
|
Output:
Path: C:\Program Files\Java\jre1.8.0_211
Program 2:
Java
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
{
Path path
= Paths.get("C:\\Users\\"
+ "asingh.one\\Documents");
System.out.println("Path: "
+ path.toString());
}
}
|
Output:
Path: C:\Users\asingh.one\Documents
References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#toString()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...