How to get parent directory name

package JavaIOExample;

import java.util.Scanner;

 * Here we will learn to check parent directory name.
public class CheckParentDirectoryName {

	public static void main(String args[]) {
		Scanner scanner = new Scanner(;
		System.out.println("Enter the file path");
		String filePath =;
		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());


Leave A Comment

Your email address will not be published. Required fields are marked *