![]() |
Java is one of the most popular and powerful programming languages. It is widely used for various purposes. In this article, we are going to learn how to find the parent directory of a file or a specified path. Methods to Get the Parent Directory of a fileIn Java, we can get the parent Directory with the help of two methods.
Program to Get the File’s Parent Directory in Java1. Using java.io.File and getParentFile()In this method, we have a path stored in a string variable now we are creating a file object. After creating a file object, we call the getParentFile() method to get the parent directory of the file object. Java
Output
Enter the file path: E:/Lokesh/Java/learn/JavaFile.java Parent Directory: E:/Lokesh/Java/learn Explanation of the above program:
2. java.nio.file.Path and getParent()In this method, we are first declaring the path of file and the creating an object of the path and after creating an object, we are getting the parent of the file path. Java
Output
Enter the file path: E:/Lokesh/Java/learn/JavaFile.java Parent Directory: E:/Lokesh/Java/learn Explanation of the above program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |