We are moved to new domain
Click -> www.ehowtonow.com
Wednesday, 24 May 2017

How to find parent directory of file using Java

String java.io.File.getParent()

Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.

The parent of an abstract pathname consists of the pathname's prefix, if any, and each name in the pathname's name sequence except for the last. If the name sequence is empty then the pathname does not name a parent directory.


Returns:
The pathname string of the parent directory named by this abstract pathname, or null if this pathname does not name a parent

ParentDirectory.java
package com.javatutorialcorner.directory;

import java.io.File;

public class ParentDirectory {

	public static void main(String[] args) {
		String filePath = "C:/JTC/Java/Examples/Directory/File/java.txt";
		File file = new File(filePath);
		String parentDirectory = file.getParent();
		System.out.println("Parent directory  : " + parentDirectory);

		if (!file.isDirectory()) {
			System.out.println("This is File");
			System.out.println("Parent directory of this file is :"
					+ file.getParent());
		} else {
			System.out.println("This is Directory");
			System.out.println("Parent directory of this directory is :"
					+ file.getParent());
		}
	}

}
Output
Parent directory : C:\JTC\Java\Examples\Directory\File
This is File
Parent directory of this file is :C:\JTC\Java\Examples\Directory\File

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to find parent directory of file using Java Rating: 5 Reviewed By: eHowToNow