How to get the Parent Directory of a File in Java

Waqas Anwar
25 June 2009

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();