如何使用scala列出资源文件夹中的所有文件 [英] How to list all files from resources folder with scala

查看:77
本文介绍了如何使用scala列出资源文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在您的资源文件夹中假定以下结构:

Assume the following structure in your recources folder:

resources
├─spec_A
| ├─AA
| | ├─file-aev
| | ├─file-oxa
| | ├─…
| | └─file-stl
| ├─BB
| | ├─file-hio
| | ├─file-nht
| | ├─…
| | └─file-22an
| └─…
├─spec_B
| ├─AA
| | ├─file-aev
| | ├─file-oxa
| | ├─…
| | └─file-stl
| ├─BB
| | ├─file-hio
| | ├─file-nht
| | ├─…
| | └─file-22an
| └─…
└─…

任务是读取给定规格 spec_X 的所有文件.出于明显的原因,我们不希望使用确切的名称作为字符串文字,而使用 Source.fromResource("spec_A/AA/…")打开代码中的数百个文件.

The task is to read all files for a given specification spec_X one subfolder by one. For obvious reasons we do not want to have the exact names as string literals to open with Source.fromResource("spec_A/AA/…") for hundreds of files in the code.

此外,此解决方案当然应该在开发环境中运行,即无需打包到jar中.

Additionally, this solution should of course run inside the development environment, i.e. without being packaged into a jar.

推荐答案

好吧,在尝试分析了收集器的API之后,我能够通过ListBuffer收集器创建一个scala.List.

Ok, after some trying and analyzing the Collector’s API I was able to create a scala.List via ListBuffer collector.

class SpecReader (val spec:String) {

  private val basePath = s"/spec_$spec"
  lazy val jarFileSystem: FileSystem = FileSystems.newFileSystem(getClass.getResource(basePath).toURI, Map[String, String]().asJava);


  def readSpecMessageScala(): String = {
    List("CN", "DO", "KF")
      .flatMap(listPathsFromResource)
      .flatMap(path ⇒ Source.fromInputStream(Files.newInputStream(path), "UTF-8").getLines())
      .reduce(_ + " " + _)
  }

  val collector: Collector[_ >: Path, ListBuffer[Path], List[Path]] =  Collector.of(
    new Supplier[ListBuffer[Path]]() {
      override def get(): ListBuffer[Path] = ListBuffer[Path]()
    },
    new BiConsumer[ListBuffer[Path], Path]() {
      override def accept(t: ListBuffer[Path], u: Path): Unit = t.addOne(u)
    },
    new BinaryOperator[ListBuffer[Path]]() {
      override def apply(t: ListBuffer[Path], u: ListBuffer[Path]): ListBuffer[Path] = t.addAll(u)
    },
    new Function[ListBuffer[Path], List[Path]](){
      override def apply(v1: ListBuffer[Path]): List[Path] = v1.toList
    },
    Array[Collector.Characteristics](): _*
)

  def listPathsFromResource(folder: String): List[Path] = {
    Files.list(getPathForResource(folder))
      .filter(p ⇒ Files.isRegularFile(p, Array[LinkOption](): _*))
      .sorted.collect(collector)
  }

  private def getPathForResource(filename: String) = {
    val url = classOf[ConfigFiles].getResource(basePath + "/" + filename)
    if ("file" == url.getProtocol) Paths.get(url.toURI)
    else jarFileSystem.getPath(basePath, filename)
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    System.out.println(new SpecReader(args.head).readSpecMessage())
  }
}

对于空的可变参数和空的设置图,需要特别注意.

special attention was necessary for the empty varargs and empty setting maps.

仍然进行测试和jar操作.Git更新,PUll请求欢迎: https://github.com/kurellajunior/list-files-from-resource-directory

Still the case for testing and jar operation. Git updated, PUll requests welcome: https://github.com/kurellajunior/list-files-from-resource-directory

这篇关于如何使用scala列出资源文件夹中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆