How to get the Parent Directory of a File in Java

If you have an absolute path of a file available in file system and you want to obtain the reference of its parent directory you can use getParent() or getParentFile() methods of File class

File file = new File("D:\\Java\\");

String parentPath = file.getParent();             

File parentDir = file.getParentFile();



About author

User avatar

Waqas Anwar

Founder of, Senior Consultant, Microsoft .NET and Web Technologies