How to Get Parent Directory in Java


In this example, we may attain the method to get the parent directory of a particular file or directory as file object.

Source Code

package com.beginner.examples;

import java.io.File;


public class GetParentFileExample {

    public static void main(String[] args){
    	
    	File file = new File("D:/Workspaces/test.txt");
        // get parent file
        File p = file.getParentFile();
        System.out.println("Parent file is : "" + p.getAbsolutePath() + "".");
    }
    
}

Output:

Parent file is : "D:Workspaces".

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments