Friday 11, Apr 2025
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

Related Posts:

  • How to compare paths of two file using Java Following example shows how to compare paths of two file using file1.compareTo() method in Java. compareTo public int compareTo(File pathname)  Compares two abstract pathnames lexicographically. The ordering defined by… Read More
  • How to find min and max value from Collection Following example shows how to find minimum and maximum of Collection using Collections.min(Collection<T> collection) and Collections.max(Collection<T> collection) method in Java. <String> String java.util… Read More
  • Java Example – String Compare Following example we are going to see about how to compare two String using compareTo(String arg) , compareToIgnoreCase(String arg)  methods available in Java . compareTo(String arg) Compares two strings lexicographica… Read More
  • How to rename the file using Java Following example shows how to rename the file using file.renameTo() method in Java. renameTo public boolean renameTo(File dest)  Renames the file denoted by this abstract pathname. Many aspects of the behavior of this… Read More
  • How to get file size using Java Following example shows how to get file size in bytes using file.length() method in Java. lengthpublic long length() Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this … Read More
  • 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