scala-logo
Let’s briefly discuss how to deal with a resources folder and directories in Scala project. It’s pretty frequent case in a programming, when you need to interact with file system, Scala isn’t an exception. So how it can be done on practice?

If you want to read about the most powerful way of reading & writing files in Scala, please follow the link.

I’m going to demonstrate a short example on a real Scala project with a such structure:

scala-folders-path-resources

As you see it has the resources folder with files and directories inside of it.

Access the resources folder

In order to get a path of files from the resources folder, I need to use following code:

object Demo {

  def main(args: Array[String]): Unit = {
    val resourcesPath = getClass.getResource("/json-sample.js")
    println(resourcesPath.getPath)

  }
}

An output of the code below, is something like this:

/Users/Alex/IdeaProjects/DirectoryFiles/out/production/DirectoryFiles/json-sample.js

Read file line by line

In some sense a pure path to a file is useless. Let’s try to read the file from resources line by line. Scala can do it well:

import scala.io.Source

object Demo {

  def main(args: Array[String]): Unit = {

    val fileStream = getClass.getResourceAsStream("/json-sample.js")
    val lines = Source.fromInputStream(fileStream).getLines
    lines.foreach(line => println(line))

  }

}

The output is

{
    "name": "Alex",
    "age": 26
}

List files in directory

The final important and popular task is to list files from a directory.

import java.io.File

object Demo {

  def main(args: Array[String]): Unit = {

    val path = getClass.getResource("/folder")
    val folder = new File(path.getPath)
    if (folder.exists && folder.isDirectory)
      folder.listFiles
        .toList
        .foreach(file => println(file.getName))

  }
}

Looks simple and efficient as well.

I hope this article was useful for you. Leave your comments and like us on facebook!

About The Author

Mathematician, programmer, wrestler, last action hero… Java / Scala architect, trainer, entrepreneur, author of this blog

  • Alexander Glukhovtsev

    Is it preferred way ?
    And what about scala.io.Source?

    • I described here java-style
      Of course you can use much concise way such as:

      Source.fromFile(“file.txt”)

      • Alexander Glukhovtsev

        I was reading tutorial about Swing
        and the interesting part for me was about ImageIO
        In particular,
        how I can access the same properties as with ImageReaders in Scala way.

        • Actually if you have a java code sample of something, you can repeat it with Scala syntax, because Scala supports Java libs and as you may guess a lib’s API remains the same 🙂

          • Alexander Glukhovtsev

            All joking aside,
            once upon a time I did the opposite
            translating the Scala snippets into Java
            (after that I do not like Java anymore).

          • Well agree 🙂
            I prefer Scala over Java too, but sometimes still need to use old J 😀

Close