Scala: How to get file list inside resources?

Sometimes it is necessary to list all files, or files with specific suffix, inside resources.

It should work for file system in the develop stage as well for JAR in production.

A solution is fileNames method:

/**
  * Searches in the resources a list of file names matching a file name suffix
  *
  * @param fileNameSuffix the file name suffix; empty - all files
  * @param folder         specifies a folder; default value is "."
  * @return the list of file names matching a file name suffix
  */
def fileNames(fileNameSuffix: String, folder: String = "."): Seq[String] = {
  new File(Thread
    .currentThread()
    .getContextClassLoader
    .getResource(folder)
    .getPath)
    .listFiles(new FileFilter {
      override def accept(pathname: File): Boolean = {
        pathname.isFile && pathname.getName.endsWith(fileNameSuffix)
      }
    })
    .map(file => file.getName)
}

Code source is here: https://github.com/ysden123/ys-scala-tools/blob/master/src/main/scala/com/stulsoft/scala/tools/resources/ResourceUtils.scala

Advertisements
Posted in Programming, Scala | Tagged , , , | Leave a comment

Buds

Buds
Image | Posted on by | Tagged , , | Leave a comment

Date

Date fruit in contrast:

Date
Posted in Photography, Photos | Tagged , , , , | Leave a comment

After Purim

Masks after Purim:

Mask
Mask
Posted in Photography, Photos | Tagged , , | Leave a comment

Flowers

Preparation to night

Close to home
Posted in Photography, Photos | Tagged , , | 3 Comments

Thorns

Thorns, sketch

Thorns
Posted in Photography, Photos | Tagged , , | Leave a comment

Pelican takes a rest

Pelican
Image | Posted on by | Tagged , , , | Leave a comment

Purim

Purim. Sderot Rotshild, Tel Aviv, 2019

Purim
Posted in Photography, Photos | Tagged , , | Leave a comment

Old photo of sea food

This is an old photo, but from this it did not become less tasty.

Sea food
Posted in Photography, Photos | Tagged , , , | Leave a comment

Israeli forest

Israeli landscape. Ben Shemen forest:

Israeli forest
Posted in Photography, Photos | Tagged , , , | Leave a comment