How to Convert a String to a Path in Java? Last Updated : 19 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, we can convert a String representation of a file or directory path into a path object using the Paths utility class. It is a part of the java.nio.file package. The Paths class provides a method called get(String file path location) that converts a sequence of strings representing a path into a Path object. In this article, we will learn How to Convert a String to a Path in Java. Import these classes: import java.nio.file.Paths;import java.nio.file.Path;Program to Convert a String to a Path in JavaWe can use the Paths.get() method to convert the string to a path. Java // Java Program to Convert a String // To a Path Using Paths.get() method import java.nio.file.Path; import java.nio.file.Paths; // Driver Class class GFG { // Main Function public static void main(String args[]) { // Define the String representing the File Path String location = "C:/Users/Desktop/New Folder"; // Convert the string to a Path object Path path = Paths.get(location); // Output the path System.out.println(path); } } OutputC:/Users/Desktop/New Folder Explanation of the Program:We define a String variable location containing the file path.Then we have used the static Paths.get() method to convert the location string into a Path object. 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. By using the Paths.get() method and pass each folder as a separate parameter. Java import java.nio.file.Path; import java.nio.file.Paths; // Driver Class class GFG { // Main Function public static void main(String args[]) { // Creating Path Object Piece by Piece // Using Paths.get() Path path = Paths.get("C:", "Users", "Desktop" ,"New Folder"); System.out.println(path); } } OutputC:/Users/Desktop/New Folder Explanation of the Program:In the above program, We have used the static Paths.get() method to create a Path object. Again, this method takes multiple string arguments representing individual elements of the path.Here, each argument represents a component of the path, such as directory name, file name etc.The resulting Path object represents the concatenation of these elements by forming the complete path. Comment More infoAdvertise with us Next Article How to Convert a String to a Path in Java? S skaftafh Follow Improve Article Tags : Java Java-Strings Java Examples Practice Tags : JavaJava-Strings Similar Reads Convert a String to a List of Characters in Java In Java, to convert a string into a list of characters, we can use several methods depending on the requirements. In this article, we will learn how to convert a string to a list of characters in Java.Example:In this example, we will use the toCharArray() method to convert a String into a character 3 min read How to Set Java Path in Windows and Linux? PATH is an environment variable that is used by Operating System to locate the exe files (.exe) or java binaries ( java or javac command). The path once it is set, cannot be overridden. The PATH variable prevents us from having to write out the entire path to a program on the Command Line Interface 4 min read How to set Temporary and Permanent Paths in Java In Java, the most important task is to set the environment variable on the system. If the Java source file is inside the jdk/bin folder, the path is not required to be set because tools like javac, and java can be accessed globally on the system. If the Java source file is outside the jdk/bin folder 3 min read JavaScript - How to Globally Replace a Forward Slash in a String? Here are the various methods to globally replace a forward slash in the JavaScript string.1. Using replace() Method with a Regular ExpressionThe replace() method is commonly used with a regular expression to replace all occurrences of a forward slash.JavaScriptconst s1 = "path/to/some/resource"; con 2 min read Path toString() method in Java with Examples The Java Path interface was added to Java NIO in Java 7. toString() method of java.nio.file.Path used to return the string representation of this path. If this path was created by converting a path string using the getPath method then the path string returned by this method may differ from the origi 2 min read Like