title: Finding a the "sub-path" in a hierarchical system, Java
body: <p>I have this Hierarchical file system that is built like so:</p>
  <pre><code>class StorageUnit{
      String name;
      StorageUnit(String nameInput){
          this.name = nameInput;
      }
  }
  
  class Folder extends StorageUnit{
      ArrayList&lt;StorageUnit&gt; items = new ArrayList&lt;StorageUnit&gt;();
      
      void addContent(StorageUnit item){
           this.items.add(item);
      }
  }
  
  class File extends StorageUnit{}
  </code></pre>
  <p>Given a path (not necessarily the complete path) for example for this system:</p>
  <pre><code>A
  |    B
  |    |    C
  |    |    |    code.java
  |    |    bye.log
  |    Aa.txt
  |    aa.py
  </code></pre>
  <p>a path be given like so:</p>
  <pre><code>B/C/code.java
  </code></pre>
  <p>I am trying to make a function return the specified <strong>File</strong> if there exists a path where the given path is part of it, for example if it finds the path:</p>
  <pre><code>A/B/C/code.java
  </code></pre>
  <p>the function would return <code>code.java</code> since the found path contains the given path,
  <strong>else</strong> it returns null.</p>
  <p>I thought about recursion, however, can't figure out what to do it if I end up in a file that doesn't contain the file am looking for:</p>
  <pre><code>//The entire code here belongs to the class StorageUnit
  
  public File findFile(String path){
          String[] paths = path.split(&quot;/&quot;);
  
          return findFile_aux(paths,0);
      }
  public File findFile_aux(String[] paths, int i){
          for(StorageItem item : this.items){
              if(item.getName().equals(paths[i])){
                  if(i == paths.length-1) {
                      return ((File) item);
                  }
                  item.findFile_aux(paths, i+1);
              }
              item.findFile_aux(paths, i);
          }
          return null;
      }
  </code></pre>