8:44 pm - Monday June 26, 2017

Java Program used to find the parent Directory of the mentioned file using getParent() method.

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Get Parent program to find the parent Directory of the mentioned file.

Example Program


package service1;
import java.io.File;

public class GetParent {
	public static void main(String[] args) {
		File f = null;
		String v;
		boolean bool = false;
		try {
			// create new file
			f = new File("d:\\c.txt");

			// get file name or directory name
			v = f.getParent();

			// true if the file path exists
			bool = f.exists();

			// if file exists
			if (bool) {
				// prints
				System.out.print("parent name: " + v);
			}
		} catch (Exception e) {
			// if any error occurs
			e.printStackTrace();
		}
	}
}

Output

parent name: d:\

Description

The java.io.File.getParent() method returns the pathname string if this abstract path name’s parent or null if this pathname does not name a parent directory.

Declaration

Following is the declaration for java.io.File.getParent() method:

public String getParent()

Parameters

        Not Applicable.

Return Value

This method returns pathname string of the parent directory named by this abstract pathname, or null if the pathname does not name a parent.

Exception

         Not Applicable.

BACK

Filed in: Z-A All Java Codes

Comments are closed.