Java Examples - Get the Parent Directory of a File
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 running the above code is:
文件的上级目录为 : FileOther Extensions
Java Examples