In Scala, find the files matching the String pattern.

How to get Array[io.BufferedSource]to all files that match the pattern in the given directory?

Namely, how to determine a method io.Source.fromDirsuch that

val txtFiles: Array[io.BufferedSource] = io.Source.fromDir("myDir/*.txt") // ???

FileUtils is noted in Apache Commons IO , but the Scala API approach without external dependencies is much preferable.

+4
source share
4 answers
scala> import reflect.io._, Path._
import reflect.io._
import Path._

scala> val r = """.*\.scala""".r
r: scala.util.matching.Regex = .*\.scala

scala> "/home/amarki/tmp".toDirectory.files map (_.name) flatMap { case n @ r() => Some(n) case _ => None }
res0: Iterator[String] = non-empty iterator

scala> .toList
res1: List[String] = List(bobsrandom.scala, ...)

or recursive

scala> import PartialFunction.{ cond => when }
import PartialFunction.{cond=>when}

scala> "/home/amarki/tmp" walkFilter (p => p.isDirectory || when(p.name) {
     | case r() => true })
res3: Iterator[scala.reflect.io.Path] = non-empty iterator
+6
source

Using Java 8, you can go through the directory and all its subdirectories. Then convert the iterator to scala and then filter according to files ending in .txt:

import scala.collection.JavaConverters._ java.nio.file.Files.walk(Paths.get("mydir")).iterator().asScala.filter(file => file.toString.endsWith(".txt")).foreach(println)

+2

, - :

def getFilesMatchingRegex(dir: String, regex: util.matching.Regex) = {
    new java.io.File(dir).listFiles
        .filter(file => regex.findFirstIn(file.getName).isDefined)
        .map   (file => io.Source.fromFile(file))
}

Note that this will not extract files in subdirectories, it no longer has the pre-hover features that you would expect (Γ  la ls ./**/*.scala), etc.

+1
source

Here is the answer to this excellent answer from @ som-snytt :

scala> import reflect.io._, Path._
import reflect.io._
import Path._

scala> "/temp".toDirectory.files.map(_.path).filter(name => name matches """.*\.xlsx""")
res2: Iterator[String] = non-empty iterator

as an array:

scala> "/temp".toDirectory.files.map(_.path).filter(name => name matches """.*\.xlsx""").toArray
res3: Array[String] = Array(/temp/1.xlsx, /temp/2.xlsx, /temp/3.xlsx, /temp/a.1.xlsx, /temp/Book1.xlsx, /temp/new.xlsx)
+1
source

Source: https://habr.com/ru/post/1568117/


All Articles