Get parent directory as a File object in Java
Description
The following code shows how to get parent directory as a File object.
Example
// w w w.ja v a2 s.c o m
import java.io.File;
public class Main {
public static void main(String[] args) {
File file = new File("C:/demo.txt");
File fileParent = file.getParentFile();
System.out.println("Parent directory: " + fileParent.getPath());
}
}
The code above generates the following result.