Java IO & NIO - How to find parent directory by a child file name, if another nested level siblings file path is known

Java IO & NIO Java 

This example demonstrate how to find a parent folder file by it's child name given that another file is known to exist under the same parent but under another nested level.

In this particular example, we want to find a maven project root directory given that we know that it's always a parent of a 'pom.xml' file. Another Java file location of the same project is also known.

package com.logicbig.example;


public class FindParentTest {

public static void main (String[] args) {
File file = new File("C:\\projects\\test-project\\src\\" +

File parent = findParentDirBySiblingName(file, "pom.xml");

* @param thisFile a known File Somewhere Under Target Parent
* @param siblingName search by name
* @return
public static File findParentDirBySiblingName (File thisFile,
String siblingName) {

File parent = thisFile.getParentFile();

while (parent != null) {
File file = new File(parent.getAbsolutePath() + File.separatorChar + siblingName);
if (file.exists()) {
return parent;
parent = parent.getParentFile();
return null;



See Also