static public String locateRelative ( String relpath , String abspath , boolean wantdir ) { // clean up the path and filename relpath = relpath . trim ( ) . replace ( ' ' , ' ' ) ; if ( relpath . charAt ( 0 ) == ' ' ) relpath = relpath . substring ( 1 ) ; if ( relpath . endsWith ( "/" ) ) relpath = relpath . substring ( 0 , relpath . length ( ) - 1 ) ; String [ ] pieces = relpath . split ( "[/]" ) ; String partial = abspath ; for ( int i = 0 ; i < pieces . length - 1 ; i ++ ) { String nextdir = locateFile ( pieces [ i ] , abspath , true ) ; if ( nextdir == null ) return null ; partial = nextdir ; } // See if the final file|dir exists in this dir String finalpath = locateFile ( pieces [ pieces . length - 1 ] , partial , wantdir ) ; return finalpath ; }
Walk the specified dir tree to locate file specified by relative path . Use breadth first search .