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
🌐
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 ...
🌐
Medium
medium.com › @AlexanderObregon › javas-paths-get-method-explained-9586c13f2c5c
Java’s Paths.get() Method Explained | Medium
September 8, 2024 - Converting Paths: Sometimes, you may need to convert a Path object to a string or URI for further processing or logging.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › pathOps.html
Path Operations (The Java™ Tutorials > Essential Java Classes > Basic I/O)
To clean up a path while ensuring ... to convert the Path. If you need to convert the path to a string that can be opened from a browser, you can use toUri....
🌐
Dev.java
dev.java › learn › java-io › file-system › path
Working with Paths - Dev.java
January 25, 2023 - This method is described in the next section. You can use three methods to convert the Path. If you need to convert the path to a string that can be opened from a browser, you can use toUri().
🌐
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 - Path path = Paths.get("D:", "documents", "music.txt"); // if needed String strPath = path.toString(); System.out.println(strPath); ... How to capitalize the String first letter of each word in JavaConvert a string to long in JavaHow to make ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Paths.html
Paths (Java Platform SE 8 )
April 21, 2026 - The details as to how the Strings are joined is provider specific but typically they will be joined using the name-separator as the separator. For example, if the name separator is "/" and getPath("/foo","bar","gus") is invoked, then the path string "/foo/bar/gus" is converted to a Path.
Find elsewhere
🌐
Java2Blog
java2blog.com › home › core java › java path › convert string to path in java
Convert String to Path in Java - Java2Blog
September 16, 2022 - To convert String to Path, use jva.nio.file.Paths's get() method. It is a static method. ... 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. Here is an example:
🌐
javathinking
javathinking.com › blog › convert-string-to-path-java
Convert String to Path in Java — javathinking.com
The Path interface provides more ... the Path interface in new Java applications. You can use the toString() method of the Path object to convert it back to a string. For example:...
🌐
Dariawan
dariawan.com › tutorials › java › java-11-new-methods-path-and-files
Java 11 - New Methods in Path and Files | Dariawan
February 25, 2019 - Java 11 added following new methods in java.nio.file.Files class to directly read String from files and to directly write string to files: String readString(Path): Reads all content from a file into a string, decoding from bytes to characters ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-file-path-absolute-canonical
Java File Path, Absolute Path and Canonical Path | DigitalOcean
August 3, 2022 - Today we will look into the Java file path. Java File path can be abstract, absolute or canonical. java.io.File contains three methods for determining the file path, we will explore them in this tutorial. getPath(): This file path method returns the abstract pathname as String.
🌐
Jenkov
jenkov.com › tutorials › java-nio › path.html
Java NIO Path
The first example creates a Java Path instance which points to the path (directory) d:\data\projects. The second example creates a Path instance which points to the path (file) d:\data\projects\a-project\myfile.txt .
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › nio › file
java.nio.file.Path Example - Java Code Geeks
March 19, 2019 - In order to help you master Java NIO Library, we have compiled a kick-ass guide with all the major Java NIO features and use cases! Besides studying them online you may download the eBook in PDF format! Download NOW! We will contact you soon. ... The following are the example code showing the path conversion. Note the same Java source file from the previous section is used. ... public static void main (String ...
🌐
Coderanch
coderanch.com › t › 658922 › java › Easier-Path-File-Java
An Easier Way to Specify Path & File Name to Java? (Beginning Java forum at Coderanch)
December 10, 2015 - Then I'll have to convert the array to a string object... A pile of assumptions, most of which are NOT true. First: if you dislike '\'s (and I sympathise with you there), you can simply run replace('\', '/') on the string in question (as long as it's a path), because Java (NOT Windows AFAIK; unless things have changed) will convert forward slashes into back ones for you if it works out it's running on a Windows environment.
🌐
Programiz
programiz.com › java-programming › examples › get-relative-path
Java Program to Get the relative path from two absolute paths
import java.io.File; class Main { public static void main(String[] args) { // Create file objects File file1 = new File("C:\\Users\\Desktop\\Programiz\\Java\\Time.java"); File file2 = new File("C:\\Users\\Desktop"); // convert file objects to string String absolutePath1 = file1.toString(); System.out.println("Absolute Path1: " + absolutePath1); String absolutePath2 = file2.toString(); System.out.println("Absolute Path2: " + absolutePath2); // get the relative path String relativePath = absolutePath1.substring(absolutePath2.length()); System.out.println("Absolute Path: " + relativePath); } } ... Absolute Path1: C:\Users\Desktop\Programiz\Java\Time.java Absolute Path2: C:\Users\Desktop Absolute Path: \Programiz\Java\Time.java · In the above example, we have converted the file paths to strings.
🌐
Oracle
docs.oracle.com › en › java › javase › 20 › docs › api › java.base › java › nio › file › Path.html
Path (Java SE 20 & JDK 20)
July 10, 2023 - The details as to how the Strings are joined is provider specific but typically they will be joined using the name-separator as the separator. For example, if the name separator is "/" and getPath("/foo","bar","gus") is invoked, then the path string "/foo/bar/gus" is converted to a Path.