Java Path was introduced in JDK 1.6 and JDK1.7. NIO was officially added after JDK 1.7. Path Interface In the java.nio.file package, the full Path of Path is named java.nio.file.path.

Java Path instances represent paths in the file system. Path can point to any file or Path. Path can be an absolute Path or a relative Path. The absolute path contains the complete path starting from the root path of the file system. Relative path is the path of a file or directory relative to other files. Relative paths sound a little confusing. Don’t worry, I’ll cover it in detail in the Java NIO Path tutorial.

Do not confuse the path environment variable of some operating systems with the path of the file system. The java.nio.file.Path interface has nothing to do with the Path environment variable.

In many cases, java.nio.file. Path is similar to java.io. Path, but there are a few differences. In many cases, though, the File class can be replaced directly with Path.

Create Path instance

In order to use the java.nio.file.Path instance, you must first create a Path instance. You can create an instance of Path using the static paths.get () method of Path. Here’s an example:

import java.nio.file.Path;
import java.nio.file.Paths;

public class PathExample {
    public static void main(String[] args){
        Path path = Paths.get("c:\\data\\myfile.txt"); }}Copy the code

Notice the two import examples in the example. To use the Path interface and Paths class, we must import it first.

Second, notice the paths.get (“c:\data\myfile.txt”) method call. It calls the paths.get () method to create the Path instance. In other words, the path.get () method is a factory method to create an instance of Path.

Creating an absolute Path

The absolute Path is created through the factory method paths.pget () method, whose argument is the absolute Path to the file. Here is an example of creating an absolute Path:

Path path = Paths.get("c:\\data\\myfile.txt");

Copy the code

The absolute Path is ==c:\data\myfile.txt.==. In Java, double \ is required because \ is an escape symbol, which means that the following characters are displayed as they should be, without escaping. Writing two \ tells the Java compiler to write one \ in the string.

The above path is the file system path of Window. On Unix systems, the above paths could look like this:

Path path = Paths.get("/home/jakobjenkov/myfile.txt");
Copy the code

Now is the absolute path = = / home/jakobjenkov/myfile. Tx = =

If you are using this type of path on a Windows system, the path is replaced with the current drive. For example, path

/home/jakobjenkov/myfile.txt
Copy the code

It can be parsed on drive C. And then the full path of the path is going to be

C:/home/jakobjenkov/myfile.txt
Copy the code

Creating a relative Path

A relative Path is the Path of a Path relative to another file or directory. A full Path is a base Path plus a relative Path.

The Path class in Java NIO can also work in relative paths. You can create relative Paths using the paths.get (basePath,relativePath) method. Here are two examples of relative paths:

Path projects = Paths.get("d:\\data"."projects");

Path file     = Paths.get("d:\\data"."projects\\a-project\\myfile.txt");
Copy the code

The first example creates a Path to the D :\data\project directory. The second example creates a Path to the d:\data\projects\a-project\myfile.txt file.

There are two special codes used in paths when using relative paths. These codes are:

  • .
  • .

Code. Indicates the current directory. For example, if you create a relative path like this, like this:

Path currentDir = Paths.get(".");
System.out.println(currentDir.toAbsolutePath());
Copy the code

Path points to the directory where the code is currently running.

If. Appears in the middle of the path string, it simply represents the same location as the current directory. The following is an example of Path:

Path currentDir = Paths.get("d:\\data\\projects\.\a-project");
Copy the code

The final path is

This path will correspond to the path:

Copy the code

. Represents the parent directory or the upper directory. Here is an example of Path:

Path parent = Paths.get("..");
Copy the code

The parent directory returned by this method is the parent directory of the current application running.

If.. Appears in the middle of the path string and represents the directory one level above the current string, for example:

String path = "d:\\data\\projects\\a-project\\.. \\another-project";
Path parentDir2 = Paths.get(path);
Copy the code

The absolute path that the above example points to is

d:\data\projects\another-project
Copy the code

After a-projcet.. It points the current directory up and down to the other-project directory.

. And… You can use it in the paths.get () method with two arguments, as follows:

Path path1 = Paths.get("d:\\data\\projects".".\\a-project");

Path path2 = Paths.get("d:\\data\\projects\\a-project".".. \\another-project");
Copy the code

There are many other ways to handle relative paths in Java NIO. This will be covered later in the tutorial.

Path.normalize()

The normalize() method standardizes paths. Standardization means putting the And.. Remove and parse the path to which the current path string points. Here is an example of the Java path.normalize () method:

String originalPath =
        "d:\\data\\projects\\a-project\\.. \\another-project";

Path path1 = Paths.get(originalPath);
System.out.println("path1 = " + path1);

Path path2 = path1.normalize();
System.out.println("path2 = " + path2);
Copy the code

The Path example first creates a Path containing.. Path string. A path is then created with the string and printed out.

In this example, the normalize() method is called to create the Path instance, which returns a new Path instance. The new, normalized path instance is also printed out.

Here is the output from the above example:

path1 = d:\data\projects\a-project\.. \another-project path2 = d:\data\projects\another-projectCopy the code

As you can see, the standardized path does not contain ==a-project.. = =. Because it’s redundant. The deleted part does not add anything to the final absolute path.