Search code examples
stringpathniojava-7

Create a Path from String in Java7


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.


Solution

  • You can just use the Paths class:

    Path path = Paths.get(textPath);
    

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