Convert String to Path in Java

In this post, we will see how to convert String to Path in Java.

Using Paths’get() method [ Java 7 ]

To convert String to Path, use jva.nio.file.Paths's get() method. It is a static method.

Here is an example:

Output:

You can also join multiple strings using Paths‘s get() method. It will join all the String with delimiter /.

Here is an example:

Output:

Using Path’s of() method [ Java 11 ]

There is new method of() introduced in Java 11 which takes String as argument and provides the Path object.
Here is an example:

Output:

That’s all about how to convert String to Path in Java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *