title: Deal with Java NIO Iterator in Scala with Try
body: <p>I recently learned how to use Scala's native <code>Try</code> type to handle errors. One good thing with <code>Try</code> is that I'm able to use for-comprehension and silently ignore the error.</p>
  
  <p>However, this becomes a slight problem with Java's NIO package (which I really want to use).</p>
  
  <pre><code>val p = Paths.get("Some File Path")
  for {
    stream &lt;- Try(Files.newDirectoryStream(p))
    file:Path &lt;- stream.iterator()
  } yield file.getFileName
  </code></pre>
  
  <p>This would have been perfect. I intend to get all file names from a directory, and using a <code>DirectoryStream[Path]</code> is the best way because it scales really well. The NIO page says <code>DirectoryStream</code> has an <code>iterator()</code> method that returns an iterator. For Java's for loop, it's enough and can be used like this:</p>
  
  <pre><code>try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir)) {
      for (Path file: stream) {
          System.out.println(file.getFileName());
      }
  }
  </code></pre>
  
  <p>However, Scala does not accept this. I was greeted with the error:</p>
  
  <pre><code>[error] /.../DAL.scala:42: value filter is not a member of java.util.Iterator[java.nio.file.Path]
  [error] file:Path &lt;- stream.iterator
  </code></pre>
  
  <p>I try to use <code>JavaConverters</code>, and it shows it handles Java <code>Iterator</code> type: <code>scala.collection.Iterator &lt;=&gt; java.util.Iterator</code>, but when I try to call it in this way: <code>stream.iterator().asScala</code>, the method is not reachable.</p>
  
  <p>What should I do? How do I write nice Scala code while still using NIO package?</p>