yaely
yaely

Reputation: 1

How to read files from a directory and find files with a specific extension -scala

I need to read files from a directory and find files with a certain extension, so first add them to a runner that progresses with reading the files

Upvotes: 0

Views: 220

Answers (2)

Scalway
Scalway

Reputation: 1663

you can also use better files library. Here is ammonite snippet:

import $ivy.`com.github.pathikrit::better-files:3.8.0`
import better.files._ 
val mit:Iterator[T] = File("/home/scalway/some/path")
  .children.filter(_.extension.contains("zip"))

Upvotes: 0

Brian
Brian

Reputation: 20285

Welcome!

The java.io.File class can read files in a directory which can then be converted to a list and filtered by the file extension. The example below would return a list of zip files in the current directory.

new java.io.File(".").list.filter(file => file.endsWith(".zip"))

res26: Array[String] = Array(foo.zip, bar.zip)

Upvotes: 1

Related Questions