You can just use the Paths class:

Path path = Paths.get(textPath);

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

Answer from Jon Skeet on Stack Overflow
🌐
Medium
medium.com › @skaftafh › how-to-convert-a-string-to-a-path-in-java-86d528648a20
How to convert a String to a path in Java? | by SK AFTAF HOSSAIN | Medium
January 31, 2024 - more) that converts a sequence of strings representing a path into a Path object. ... import java.nio.file.Paths; import java.nio.file.Path; class Main { public static void main (String[] args) { String location = "C:/Users/Desktop/New Folder"; ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-a-string-to-a-path-in-java
How to Convert a String to a Path in Java? - GeeksforGeeks
July 23, 2025 - This method returns a Path object constructed from one or more strings that when joined form a path string. If we do not have the entire path in string, we can provide it piece by piece, or folder by folder.
🌐
Medium
medium.com › @AlexanderObregon › javas-paths-get-method-explained-9586c13f2c5c
Java’s Paths.get() Method Explained | Medium
September 8, 2024 - Alternatively, you can also create a Path object using multiple strings: Path path = Paths.get("C:", "Users", "Alexander", "Documents", "file.txt"); This method joins the parts together, resulting in the same Path object as in the previous example. Understanding the difference between relative and absolute paths is crucial when working with Paths.get(). Paths in Java can either be absolute, meaning they specify the location of a file or directory from the root of the file system, or relative, meaning they specify the location relative to another directory, typically the current working directory.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Paths.html
Paths (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... This class consists exclusively of static methods that return a Path by converting a path string or URI.
🌐
Reactgo
reactgo.com › home › how to convert a string to path in java
How to convert a String to Path in Java | Reactgo
May 19, 2023 - import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) { String strPath = "D:/documents/music.txt"; // converts string to path Path p1 = Paths.get(strPath); System.out.println(p1); } }
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › pathOps.html
Path Operations (The Java™ Tutorials > Essential Java Classes > Basic I/O)
The Path class supports equals, enabling you to test two paths for equality. The startsWith and endsWith methods enable you to test whether a path begins or ends with a particular string. These methods are easy to use.
🌐
Java2Blog
java2blog.com › home › core java › java path › convert string to path in java
Convert String to Path in Java - Java2Blog
September 16, 2022 - You can also join multiple strings using Paths‘s get() method. It will join all the String with delimiter /. ... There is new method of() introduced in Java 11 which takes String as argument and provides the Path object.
Find elsewhere
🌐
Dev.java
dev.java › learn › java-io › file-system › path
Working with Paths - Dev.java
January 25, 2023 - The method that takes a String converts the String to a Path and then calls the other method. If this path does not have a root component and the given path has a root component then this path does not end with the given path.
🌐
GeeksforGeeks
geeksforgeeks.org › java › path-tostring-method-in-java-with-examples
Path toString() method in Java with Examples - GeeksforGeeks
April 14, 2023 - Below programs illustrate toString() method: Program 1: ... // Java program to demonstrate // java.nio.file.Path.toString() method import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static ...
🌐
Baeldung
baeldung.com › home › java › java io › getting the filename from a string containing an absolute file path
Getting the Filename From a String Containing an Absolute File Path | Baeldung
January 8, 2024 - Since Java 1.7, the newer java.nio libraries ship with the Path interface. Once we have a Path object, we can get the filename by calling the Path.getFileName() method. Unlike the File class, we can create a Path instance using the static Paths.get() method. Next, let’s create a Path instance from the given PATH_LINUX string and test the solution on Linux:
🌐
Sabe
sabe.io › blog › java-convert-string-path
How to Convert a String to Path in Java - Sabe.io
October 18, 2022 - If you don't have the entire path in string, you can provide it piece by piece, or folder by folder, by using the Paths.get() method and pass each folder as a separate parameter: JAVAimport java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) { Path path = Paths.get("C:", "Users", "sabe", "Desktop"); } }
🌐
Baeldung
baeldung.com › home › java › java io › java nio2 path api
Java NIO2 Path API | Baeldung
January 8, 2024 - The helper class, java.nio.file.Paths (in plural form) is the formal way of creating Path objects. It has two static methods for creating a Path from a path string:
🌐
Jenkov
jenkov.com › tutorials › java-nio › path.html
Java NIO Path
The absolute path is c:\data\myfile.txt. The double \ characters are necessary in Java strings, since the \ is an escape character, meaning the following character tells what character is really to be located at this place in the string.
🌐
Baeldung
baeldung.com › home › java › java io › java – path vs file
Java – Path vs File | Baeldung
April 20, 2024 - In Java, Path and File are classes responsible for file I/O operations.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Path.html
Path (Java Platform SE 8 )
April 21, 2026 - InvalidPathException - If the path string cannot be converted to a Path. ... Returns a path that is this path with redundant name elements eliminated. The precise definition of this method is implementation dependent but in general it derives from this path, a path that does not contain redundant name elements.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › nio › file › Paths.html
Paths (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... This class consists exclusively of static methods that return a Path by converting a path string or URI.
🌐
Baeldung
baeldung.com › home › java › java io › comparing getpath(), getabsolutepath(), and getcanonicalpath() in java
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java | Baeldung
February 8, 2025 - This is essentially the pathname passed to the File constructor. So, if the File object was created using a relative path, the returned value from getPath() method would also be a relative path.