Dir Parent
# Java Example - Get Parent Directory of a File
[ Java Example](#)
The following example demonstrates using the `file.getParent()` method of the `File` class to get the parent directory of a file:
## Main.java File
import java.io.File; public class Main{public static void main(String[]args){File file = new File("C:/File/demo.txt"); String strParentDirectory = file.getParent(); System.out.println("The parent directory of the file is : " + strParentDirectory); }}
The output of the above code is:
The parent directory of the file is : File
[ Java Example](#)
YouTip