Java File getParent() Method
Example
Get the parent folder of two paths:
import java.io.File;
public class Main {
public static void main(String[] args) {
File myFile = new File("myFolder/filename.txt");
System.out.println(myFile.getParent());
File otherFile = new File("filename.txt");
System.out.println(otherFile.getParent());
}
}
Definition and Usage
The getParent() method returns the path of the parent directory (the folder that holds the file).
It only looks at the path text, and does not check if the folder exists. If the path has no folder part, like filename.txt, it returns null.
Syntax
public String getParent()
Parameter Values
None.
Technical Details
| Returns: | The path of the parent directory, as a String. null if the path has no parent. |
|---|
Related Pages
❮ File Methods