1. Introduction
The getParent() method returns the name of the parent directory for specified File
. If the path does not have any parent this method will return null. The method is very similar to getParentFile() but in this case we will get only the string representation of the parent folder, not the whole File
object.
2. Method signature
Parameters:
- method does not take any parameter
Returns
- String - the name of the parent directory or null if specified pathname does not have a parent
3. Examples
3.1. The program that prints the parent of specified File
objects
The output:
4. Conclusion
In this short article, we presented File.getParent()
with a simple example of how to use this method. We can use this method to retrieve the parent name of the directory up to the root folder.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}