结果
上面的代码示例将产生以下结果。
Parent directory is : File
下面是另一个获取文件父目录的例子
import java.io.File;
import java.util.Scanner;
public class CheckParentDirectory {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the file path");
String filePath = scanner.next();
File fileToTest = new File(filePath);
if (!fileToTest.isDirectory()) {
System.out.println("This is File");
System.out.println("Parent Folder of this file is :" + fileToTest.getParent());
} else {
System.out.println("This is Directory");
System.out.println(
"Parent Folder of this directory is :" + fileToTest.getParent());
}
}
}
上面的代码示例将产生以下结果。
Enter the file path
C:\Apache24\htdocs\javaexamples\dir_modification.htm
This is File
Parent Folder of this file is :C:\Apache24\htdocs\javaexamples