EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Java NIO Tutorial Java NIO Path
Secondary Sidebar
Java NIO Tutorial
  • Java NIO Tutorial
    • Java NIO Channel
    • Java NIO
    • Java nio ByteBuffer
    • Java nio file AccessDeniedException
    • Java NIO Path
    • Java NIO File
    • Java NIO Scatter/Gather
    • Java NIO SocketChannel
    • Java NIO Selector

Java NIO Path

Definition of Java NIO Path

Java nio path contains the path of the directory or file. Path name will suggest the path of a particular location or an entity like a file or directory. In java, path is nothing but the interface that introduced nio file package of java, this was introduced in java version 7. It is useful to search the specified file or directory.

Java NIO Path

Introduction to Java NIO Path

The java nio path is a representation of a particular file location. It is an entity that contains two types i.e. relative path and absolute path. Both path name suggests that the absolute path is the address location that was relative to the other path.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

To use the java nio path we need to use delimiter as its definition, for windows we need to use “\” and for Linux we need to use the “/”. The get method is used to convert the string path or a string sequence when we have joined from the path instance or string of path. This method throws an exception for an invalid path if the arguments contain illegal characters.

Key Takeaways

  • To retrieve the absolute path, we need to pass the complete directory and element list that was required for the file location.
  • To retrieve the relative path, we need to combine both the nio file path together. We can retrieve relative and absolute paths in a single code.

How to Create Java NIO Path Class?

The path instance of java represents file system path. The java nio path point to either file or a specified directory. In java nio path, absolute path contains the full path whereas the relative file path contains the path to the specified file or directory. The interface of the java nio file path is similar to the class of the java IO file with the interface of the path.

1. Create Path Instance

To use the file path of java.nio.file.Path we need to create an instance of the path. We can create the instance of the path to use the static method. The below example shows the creation file path as follows.

Code:

import java.nio.file.Path;
import java.nio.file.Paths;
public class nio_path {
public static void main(String[] args) {
Path p = Paths.get ("G:\\file.txt");
System.out.println ("File found");
}
}

Java NIO Path 1

In the above example, we use two import statements first for the path interface and another for the class. We use the Paths method to call the get method that creates the instance of the path.

2. Create Absolute Path

We can create the absolute path in the get method, we need to use the absolute file parameter. Below example shows to create an absolute path in windows OS as follows.

Code:

import java.nio.file.Path;
import java.nio.file.Paths;
public class nio_path {
public static void main(String[] args) {
Path p = Paths.get ("G:\\file.txt");
System.out.println ("Absolute path");
}
}

Java NIO Path 2

The “\\” contains the java string, which contains the escape (\ character) that shows that character is located in the string. In the above example, the absolute path is “G:\\file.txt”.

The above file path is for the windows system. We can define the path in the UNIX system. The below example shows the absolute path in the UNIX system.

Example:

Code:

Path p = Paths. get("https://cdn.educba.com/home/file.txt")

3. Create Relative Path

The relative path defines the path that points from one path to the file or directory. The full path is derived from the relative or base path. The below example shows the relative path as follows.

Code:

import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
Path p = Paths.get ("G:\\path", "file.txt");
System.out.println ("Relative path");
}
}

Java NIO Path 3

To work with the relative path, we can use the below code inside the path string. The below code shows the current directory as follows.

Code:

import java.nio.file.Path;
import java.nio.file.Paths;
public class nio_path {
public static void main(String[] args) {
Path cd = Paths.get(".");
System.out.println (cd.toAbsolutePath ());
}
}

Java NIO Path 4

4. Relativize Method

This method is used to create a new path that represents the second path. Below example shows relativize method as follows.

Code:

import java.nio.file.Path;
import java.nio.file.Paths;
public class nio_path {
public static void main(String[] args) {
Path bPath = Paths.get ("G:\\path");
Path p     = Paths.get ("G:\\path\\file.txt");
Path bpt = bPath.relativize (p);
Path ptp = p.relativize (bPath);
System.out.println (bpt);
System.out.println (ptp);
}
}

Java NIO Path 5

To add the relative path to the “G:\\path” we get the full path. It will add the relative path to the “G:\\path” then we get the relative path.

5. Normalize Method

This method is the interface of the path that is used in path normalization. Normalization is defined to remove the (.) code from the path string. The below example shows the normalized method as follows.

Code:

import java.nio.file.Path;
import java.nio.file.Paths;
public class nio_path
{
public static void main(String[] args) {
String op = "G:\\path..\\file";
Path p1 = Paths.get(op);
System.out.println("path1 = " + p1);
Path p2 = p1.normalize();
System.out.println("path2 = " + p2);
}
}

Normalize Method

The above example first creates the path string with (..) code in a middle. Then it will create the instance of the path and then it will print the  instance of the path. Then we need to call the normalize method.

Java NIO Path Methods

The java nio path contains the multiple methods as follows:

  • getFileName() – This method is used to return the object of the file system.
  • getName() – This method is used to return the element name of the file path or path object.
  • getNameCount() – This method is used to return path name elements.
  • Subpath() – This method is used to return a relative path that contains a subsequence element path.
  • getParent() – This method is used to return the parent path.
  • getRoot() – This method is used to return the component of the path.
  • toAbsolutePath() – This method is used to return the object that represents the absolute path.
  • toRealPaht – This method is used to return the real path of the specified file.
  • toFile – This method is used to return the file object that represents the path.
  • normalize() – This method is used to return the path that contains the redundant element.
  • compareTo() – This method is used to compare two different abstract paths.
  • endsWith() – This method is test the path that ends with the given path string.

The below example shows java nio path methods as follows. In the below example, we have defined getFileName and toString methods.

Code:

public class nio_path {
public static void main(String[] args) throws IOException {
Path p = Paths.get("G:\\path\\file.txt");
System.out.println (p.getFileName ());
System.out.println (p.toString ());
}
}

we have defined getFileName and toString methods

Examples of Java NIO Path

Given below are the examples mentioned:

Example #1

In the below example, we have defined the absolute path as follows.

Code:

import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.file.FileSystem;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class nio_path {
public static void main(String[] args) throws IOException {
Path p = Paths.get("G:\\path\\file.txt");
Path ab = p.toAbsolutePath ();
System.out.println ("Absolute path: " + ab);
}
}

defined the absolute

Example #2

In the below example, we have defined the relative path as follows.

Code:

import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.file.FileSystem;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class nio_path {
public static void main(String[] args) throws IOException {
Path p = Paths.get ("G:\\path\\", "file.txt");
System.out.println ("Relative path: " + p);
}
}

defined the relative path

Conclusion

It is a representation of a particular file location. In java path is nothing but the interface that introduced nio file package of java, this was introduced in java version 7. The get method is used to convert the string path or a string sequence when we have joined from the path instance or string of path.

Recommended Articles

This is a guide to Java NIO Path. Here we discuss the introduction, how to create java NIO path class, methods and examples. You can also look at the following articles to learn more –

  1. Java Projects Resume
  2. Java Garbage Collectors Types
  3. Java Projects for Final Year
  4. Java HTTP Client
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more