Create a Path from String in Java7

mat_boy picture mat_boy · Jun 4, 2013 · Viewed 288.5k times · Source

How can I create a java.nio.file.Path object from a String object in Java 7?

I.e.

String textPath = "c:/dir1/dir2/dir3";
Path path = ?;

where ? is the missing code that uses textPath.

Answer

Jon Skeet picture Jon Skeet · Jun 4, 2013

You can just use the Paths class:

Path path = Paths.get(textPath);

... assuming you want to use the default file system, of course.