title: In Java elegant way to get each section of path up to Root
body: <p>Is there a more elegant way to get each section of path up to Root
  e.g given </p>
  
  <pre><code>E:\AllMusic\The Shadows\The Very Best of The Shadows
  </code></pre>
  
  <p>I want to get </p>
  
  <pre><code>E:\AllMusic\The Shadows\The Very Best of The Shadows
  E:\AllMusic\The Shadows
  E:\AllMusic
  E:\
  </code></pre>
  
  <p>I have done it with code below (I am just printing out path but in real code need to do something with these paths), but it seems very convoluted. I do want to do this properly, and note it has to work with Windows/Unix etc so I dont want to be doing clever hacks with Strings. Im using Java 8.</p>
  
  <pre><code>System.out.println(folder);
  while(folder.getNameCount()&gt;1)
  {
      if(folder.getRoot()!=null)
      {
          folder = folder.getRoot().resolve(folder.subpath(0, folder.getNameCount() - 1));
      }
      System.out.println(folder);
  }
  if(folder.getRoot()!=null)
  {
      System.out.println(folder.getRoot()); 
  }
  </code></pre>