Java NIO Path

Source: Internet
Author: User

The Java Path interface is part of the Java NIO 2 Update which Java NIO received in Java 6 and Java 7. The Java Path interface is added to Java NIO in Java 7. The Path interface is located in the Java.nio.file package, so the fully qualified name of the Java Path interface is Java . Nio.file.Path.

A Java path instance represents a path in the file system. A path can point to either a file or a directory. A path can be absolute or relative. An absolute path contains the full path from the root of the "file system down" to the "file" or directory it points to. A relative path contains the path to the file or directory relative to some other path. Relative paths may sound a bit confusing. Don ' t worry. I'll explain relative paths in + detail later in this Java NIO Path tutorial.

Confuse a file system path with the PATH environment variable in some operating systems. The Java.nio.file.Path interface has no to does with the PATH environment variable.

In many ways the Java.nio.file.Path interface was similar to the Java.io.File class, but there was some minor differences. In many cases though, you can replace the use of the File class with the use of the Path interface.

Creating a Path Instance

In order to use a Java.nio.file.Path instance you must create a Path instance. You create a Path instance using a static method in the Paths class (java.nio.file.Paths) named Paths.get (). Here is a Java paths.get () Example:

Import Java.nio.file.Path; Import java.nio.file.Paths;  Public class pathexample {    publicstaticvoid  main (string[] args) {        = Paths.get ("C:\\data\\myfile.txt");}    }

Notice the statements at the top of the example. To use the Path interface and the Paths class we must first import them.

Second, notice the Paths.get ("C:\\data\\myfile.txt") method call. It's the call to the Paths.get () method, that creates the Path instance. The Paths.get () method is a factory method for Path instances, in other words.

Creating an Absolute Path

Creating an absolute path was done by calling the Paths.get () factory method with the absolute file as parameter. Example of creating a path instance representing an absolute path:

Path PATH = Paths.get ("C:\\data\\myfile.txt");

The absolute path is c:\data\myfile.txt. The double \ characters is necessary in Java strings, since the ' is an escape character, meaning the following character Tells what character was really to being located at the the the string. by writing \ The "the Java compiler to write a" and "character into the" string.

The above path is a Windows file system path. On a Unix system (Linux, MacOS, FreeBSD etc.) the above absolute path could look like this:

Path PATH = Paths.get ("/home/jakobjenkov/myfile.txt");

The absolute path is now/home/jakobjenkov/myfile.txt.

If you used this kind of path in a Windows machine (a path starting with/) the path would is interpreted as relative to T He current drive. For instance, the path

/home/jakobjenkov/myfile.txt

Could is interpreted as being located on the C drive. Then the path would correspond to this full path:

C:/home/jakobjenkov/myfile.txt

Creating a Relative Path
A relative path is a path, which points from one path (the base path) to a directory or file. The full path (the absolute path) of a relative path is derived by combining the base path with the relative path.

The Java NIO Path class can also is used to work with relative paths. You create a relative path using the Paths.get (BasePath, RelativePath) method. Here is the relative path examples in Java:

Path projects = Paths.get ("D:\\data", "projects"); Path file     = Paths.get ("D:\\data", "projects\\a-project\\myfile.txt");

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.

When working with relative paths there is the special codes you can use inside the path string. These codes is:

    • .
    • ..

.The code means "current directory". For instance, if you create a relative path like this:

Path Currentdir = Paths.get (".") ); System.out.println (Currentdir.toabsolutepath ());

Then the absolute path, the Java instance corresponds to'll be, the Path directory in which the application executing the Above code is executed.

If the is used in the . middle of a path string it just means the same directory as the path were pointing to on that P Oint. Here is a Path example illustrating that:

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

This path would correspond to the path:

D:\data\projects\a-project

The.. Code means "parent directory" or "one directory Up". Here is a Path to Java example illustrating that:

Path Parentdir = Paths.get ("..");

The Path instance created by this example would correspond to the parent directory of the directory from which the Applica tion running this code is started.

If you use the. The code in the middle of a path string it would correspond to changing one of the directory up at this point in the path string. For instance:

String path = "d:\\data\\projects\\a-project\\." \\another-project "= paths.get (path);

The Java path instance created by this example would correspond to this absolute Path:

D:\data\projects\another-project

The.. Code after the A-project directory changes directory up the "parent directory projects and then the" Path references Dow n into the Another-project directory from there.

The. and.. Codes also work in combination with the two-string Paths.get () method. Here is the Java paths.get () examples showing simple examples of:

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

There is more ways that the Java NIO Path class can is used to work with relative paths. You'll learn more on that's later in this tutorial.

Path.normalize ()

The normalize () method of the path interface can normalize a path. Normalizing means that it removes all the. and.. Codes in the middle of the path string, and resolves what path the path is string refers to. Here is a Java path.normalize () Example:

String OriginalPath =        "d:\\data\\projects\\a-project\\. \\another-project "= paths.get (originalPath); System.out.println ("path1 =" += path1.normalize (); System.out.println ("path2 =" + path2);

This path example first creates a Path string with a. Code in the middle. Then the example creates a path instance from this path string, and prints that Path instance out (actually it prints Path . toString ()).

The example then calls normalize () on the created path instance, which returns a new path instance. This new, normalized Path instance was then also printed out.

Here are the output printed from the above example:

path1 = d:\data\projects\a-project\. \another-= D:\data\projects\another-project

As can see, the normalized path does not contain the a-project\. Part, as this is redundant. The removed part adds nothing to the final absolute path.

Java NIO Path

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.