0

I'd like to use NIO.2 features with Scala (classes are in java.nio.file):

In Java I would do:

Files.newDirectoryStream(Paths.get("/tmp"), new DirectoryStream.Filter<Path>() {
  @Override
  public boolean accept(Path entry) throws IOException {
    return false;
  }
});

How can I do same in Scala? The Fitler is static interface insideDirectoryStream interface.

Thank you.

EDIT: Please do not reply if you want to suggest me another library/method for listing files. I'm mainly interested in the main problem.

1 Answer 1

5

Not 100% sure whether you are asking for a literal conversion of this code in scala:

Files.newDirectoryStream(Paths.get("/tmp"), new DirectoryStream.Filter[Path] {
  def accept(entry: Path) = false
})

..or something else? You could add an implicit conversion:

class PathPredicateFilter(p: Path => Boolean) 
  extends DirectoryStream.Filter[Path] {
  def accept(entry: Path) = p(entry)
}
implicit def PathPredicate_Is_DirectoryStreamFilter(p: Path => Boolean) 
  = new PathPredicateFilter(p)

Now you can do this:

Files.newDirectoryStream(Paths.get("/tmp"), (p: Path) => false /* your code here */)
Sign up to request clarification or add additional context in comments.

1 Comment

Damnit. Yes I wanted literal conversion because I think I've tried every possible syntax combination and googled very hard. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.