11:19 pm - Sunday February 19, 2017

Get parent file program to get the name of parent file path using getParentFile() 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 file program to get the name of parent file path using getParentFile() method

EXAMPLE PROGRAM


import java.io.File;

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

			// returns abstract parent pathname
			f1 = f.getParentFile();

			// absolute path from abstract pathname
			v = f1.getAbsolutePath();

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

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

Output

 

Parent file path: d:\

 

Description

 

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

Declaration

 

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

public File getParentFile()

 

Parameters

 

  • Not Applicable.

 

Return Value

 

This method returns abstract pathname 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.