Path toString() method in Java

The Path class includes various methods that can be used to obtain information about the path, access elements of the path, convert the path to other forms, or extract portions of a path.

Path interface is introduced in java7.

toString() method of java.nio.file.Path used to return the string representation of this path.

path.toString()

import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class PathToStringExample { public static void main(String[] args) throws IOException { // create a Path Object Path path = Paths.get("C:\\Users\\balauser"); // print path System.out.println("Path is : " + path.toString()); } } output: Path is : C:\Users\balauser

Get the current directory path as a string

In the files system, we have.   and .. 

 . means the current directory.

.. means outer directory.

so we have
Path currentDir = Paths.get(".");

import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class PathToStringExample { public static void main(String[] args) throws IOException { Path currentDir = Paths.get("."); System.out.println("Path is : " + currentDir.toAbsolutePath().toString()); } } output: Path is : /home/cg/root/8639990/.

Post/Questions related to Path toString() method in Java

How to pass the file path as a parameter in java?
How to give a log file path in java?
How to split the file path in java?
How to get the file name from a path in java?
How to get an absolute path in the java web application?
How to get an absolute path in java?
How do I change the path of a file in Java?
How to read a file from a specified path in java?
How to convert string to the path in java?
How to get a file from a path in java? Create New File in Java
Read file to byte[] array in Java Java File Reader Examples
Java file writer Example
java.nio.file.Files methods with Examples
Java File Path, Absolute Path and Relative Path
Path toString() method in Java

In this article, we have seen the Path toString() method in Java with Examples.