- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to get the name of the parent directory of the file or directory
The name of the parent directory of the file or directory can be obtained using the method java.io.File.getParent(). This method returns the parent directory path name string or null if there is no parent named.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) { File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java"); System.out.println("File: " + file); System.out.println("Parent: " + file.getParent()); } }
The output of the above program is as follows −
Output
File: C:/jdk11.0.2/demo1.java Parent: C:/jdk11.0.2
Now let us understand the above program.
The getParent() method is used to print the name of the parent directory of the file. Also, the file is printed. A code snippet that demonstrates this is given as follows −
File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java"); System.out.println("File: " + file); System.out.println("Parent: " + file.getParent());
- Related Articles
- Java Program to get name of parent directory
- Java Program to get name of specified file or directory
- Java Program to get the File object with the absolute path for the directory or file
- Java Program to delete file or directory
- C# Program to get the name of root directory
- Get the absolute path for the directory or file in Java
- Delete the file or directory in Java
- Java Program to rename a file or directory
- Java Program to delete a file or directory when the program ends
- Java Program to get the content of a directory
- C# Program to display the name of the directory
- Rename file or directory in Java
- How do I get the parent directory in Python?
- Golang Program to get the list the name of files in a specified directory
- Java Program to check if a file or directory is readable

Advertisements