Java.io.File.getParentFile() Method
Advertisements
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
NA
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
NA
Example
The following example shows the usage of java.io.File.getParentFile() method.
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
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();
}
}
}
Let us compile and run the above program, this will produce the following result:
Parent file path: C:\