Easy Tutorial
❮ Method Factorial Number Sqrt ❯

Java Example - Get Parent Directory of a File

Java Example

The following example demonstrates how to use 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("Parent directory of the file is : " + strParentDirectory);
    }
}

The output of the above code is:

Parent directory of the file is : File

Java Example

❮ Method Factorial Number Sqrt ❯