title: Java file path as directory path
body: <p>Given such file path:</p>
  
  <pre><code>Path filePath; // "/Users/me/Desktop/archive.zip"
  </code></pre>
  
  <p>I want a function to convert this file path into directory path by removing its file extension, treating file path as a directory path. E.g. such as:</p>
  
  <pre><code>Path dirPath = asDirectoryPath(filePath); // "/Users/me/Desktop/archive"
  </code></pre>
  
  <p>What have I tried?</p>
  
  <p>Well, I have a solution, which is kinda ugly in my opinion:</p>
  
  <pre><code>private Path asDirectoryPath(Path filePath)
  {
      return Paths.get(path.toString().substring(0, path.toString().lastIndexOf('.')));
  }
  </code></pre>
  
  <p>Why ugly? Because I am forced to perform a path to string transform and search for a '.' symbol, assuming following characters are file extension. This function should be portable and should be work in both windows and unix systems. Are there any more correct solutions? Thanks.</p>