How do I list all files in a subdirectory in scala?

后端 未结 19 1514
旧巷少年郎
旧巷少年郎 2020-11-28 03:35

Is there a good \"scala-esque\" (I guess I mean functional) way of recursively listing files in a directory? What about matching a particular pattern?

For example re

19条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 04:07

    Scala is a multi-paradigm language. A good "scala-esque" way of iterating a directory would be to reuse an existing code!

    I'd consider using commons-io a perfectly scala-esque way of iterating a directory. You can use some implicit conversions to make it easier. Like

    import org.apache.commons.io.filefilter.IOFileFilter
    implicit def newIOFileFilter (filter: File=>Boolean) = new IOFileFilter {
      def accept (file: File) = filter (file)
      def accept (dir: File, name: String) = filter (new java.io.File (dir, name))
    }
    

提交回复
热议问题