How to Get the File’s Parent Directory in Java?
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 file
In Java, we can get the parent Directory with the help of two methods.
- Using java.io.File and getParentFile()
- Using java.nio.file.Path and getParent()
Program to Get the File’s Parent Directory in Java
1. 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
// Java program to get the file's parent directory // Using java.io.File and getParentFile() import java.io.File; public class lokesh { public static void main(String[] args) { // specify the file path String filePath = "E:/Lokesh/Java/learn/JavaFile.java" ; System.out.println( "Enter the file path: " +filePath); // create a File object File file = new File(filePath); // get the parent directory as a File object File parentDirectory = file.getParentFile(); if (parentDirectory != null ) { System.out.println( "Parent Directory: " + parentDirectory); } else { System.out.println( "The file does not have a parent directory." ); } } } |
Output
Enter the file path: E:/Lokesh/Java/learn/JavaFile.java Parent Directory: E:/Lokesh/Java/learn
Explanation of the above program:
- We have imported the necessary classes from the
java.io
package. - Define a class named
lokesh
. - In the
main
method:- Specify the file path as a string (
filePath
). - Create a
File
object usingnew File(filePath)
to represent the file. - Use the
getParentFile()
method of theFile
object to obtain the parent directory as anotherFile
object (parentDirectory
). - Check if the parent directory is not null. If it’s not null, print the parent directory path. Otherwise, print a message indicating that the file does not have a parent directory.
- Specify the file path as a string (
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
// Java program to get the file's parent directory // Using java.nio.file.Path and getParent() import java.nio.file.Path; import java.nio.file.Paths; public class GetParentDirectoryExample { public static void main(String[] args) { // specify the file path String filePath = "E:/Lokesh/Java/learn/JavaFile.java" ; System.out.println( "Enter the file path: " +filePath); // create a Path object Path path = Paths.get(filePath); // get the parent directory as a Path object Path parentDirectory = path.getParent(); if (parentDirectory != null ) { System.out.println( "Parent Directory: " + parentDirectory); } else { System.out.println( "The file does not have a parent directory." ); } } } |
Output
Enter the file path: E:/Lokesh/Java/learn/JavaFile.java Parent Directory: E:/Lokesh/Java/learn
Explanation of the above program:
- We have imported the necessary classes from the
java.nio.file
package. - Define a class named
GetParentDirectoryExample
. - In the
main
method:- Specify the file path as a string (
filePath
). - Create a
Path
object usingPaths.get(filePath)
to represent the file path. - Use the
getParent()
method of thePath
object to obtain the parent directory as anotherPath
object (parentDirectory
). - Check if the parent directory is not null. If it’s not null, print the parent directory path. Otherwise, print a message indicating that the file does not have a parent directory.
- Specify the file path as a string (