importjava.io.File;importjava.util.Scanner;publicclassCheckParentDirectory{publicstaticvoidmain(String args[]){Scanner scanner =newScanner(System.in);System.out.println("Enter the file path");String filePath = scanner.next();File fileToTest =newFile(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 FileParentFolder of this file is :C:\Apache24\htdocs\javaexamples