
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Name of Parent Directory in Java
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 pathname 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 + "JavaProgram" + File.separator, "demo1.txt"); System.out.println("File: " + file); System.out.println("Parent: " + file.getParent()); } }
The output of the above program is as follows −
Output
File: C:\JavaProgram\demo1.txt Parent: C:\JavaProgram
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 + "JavaProgram" + File.separator, "demo1.txt"); System.out.println("File: " + file); System.out.println("Parent: " + file.getParent());
Advertisements