How to read files from resources folder in Scala?

ScalaScala Collections

Scala Problem Overview

I have a folder structure like below:

- main
-- java
-- resources 
-- scalaresources
--- commandFiles 

and in that folders I have my files that I have to read. Here is the code:

def readData(runtype: String, snmphost: String, comstring: String, specificType:  String): Unit = {
  val realOrInvFile = "/commandFiles/snmpcmds." +runtype.trim // these files are under commandFiles folder, which I have to read. 
    try {
      if (specificType.equalsIgnoreCase("Cisco")) {
        val specificDeviceFile: String = "/commandFiles/snmpcmds."+runtype.trim+ ".cisco"
        val realOrInvCmdsList = => line.startsWith("#")).map{
          //some code 
        val specificCmdsList = => line.startsWith("#")).map{
          //some code
    } catch {
      case e: Exception => e.printStackTrace

Scala Solutions

Solution 1 - Scala

Resources in Scala work exactly as they do in Java. It is best to follow the Java best practices and put all resources in src/main/resources and src/test/resources.

Example folder structure:

├── build.sbt
├── src
│   └── main
│       ├── resources
│       │   └── readme.txt

Scala 2.12.x && 2.13.x reading a resource

To read resources the object Source provides the method fromResource.

val readmeText : Iterator[String] = Source.fromResource("readme.txt").getLines

reading resources prior 2.12 (still my favourite due to jar compatibility)

To read resources you can use getClass.getResource and getClass.getResourceAsStream .

val stream: InputStream = getClass.getResourceAsStream("/readme.txt")
val lines: Iterator[String] = stream ).getLines

nicer error feedback (2.12.x && 2.13.x)

To avoid undebuggable Java NPEs, consider:

import scala.util.Try

object Example {

  def readResourceWithNiceError(resourcePath: String): Try[Iterator[String]] = 
      .recover(throw new FileNotFoundException(resourcePath))

good to know

Keep in mind that getResourceAsStream also works fine when the resources are part of a jar, getResource, which returns a URL which is often used to create a file can lead to problems there.

in Production

In production code I suggest to make sure that the source is closed again.

Solution 2 - Scala

For Scala >= 2.12, use Source.fromResource:"located_in_resouces.any")

Solution 3 - Scala

One-liner solution for Scala >= 2.12

val source_html = Source.fromResource("file.html").mkString

Important taken from comments (thanks to @anentropic): with Source.fromResource you do not put the initial forward slash.

Solution 4 - Scala


object Demo {

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

    val ipfileStream = getClass.getResourceAsStream("/folder/a-words.txt")
    val readlines = Source.fromInputStream(ipfileStream).getLines
    readlines.foreach(readlines => println(readlines))



Solution 5 - Scala

The required file can be accessed as below from resource folder in scala

val file ="src/main/resources/app.config").getLines().mkString

Solution 6 - Scala

For Scala 2.11, if getLines doesn't do exactly what you want you can also copy the a file out of the jar to the local file system.

Here's a snippit that reads a binary google .p12 format API key from /resources, writes it to /tmp, and then uses the file path string as an input to a spark-google-spreadsheets write.

In the world of sbt-native-packager and sbt-assembly, copying to local is also useful with scalatest binary file tests. Just pop them out of resources to local, run the tests, and then delete.

import{File, FileOutputStream}
import java.nio.file.{Files, Paths}

def resourceToLocal(resourcePath: String) = {
  val outPath = "/tmp/" + resourcePath
  if (!Files.exists(Paths.get(outPath))) {
    val resourceFileStream = getClass.getResourceAsStream(s"/${resourcePath}")
    val fos = new FileOutputStream(outPath)
      Stream.continually( !=).map(_.toByte).toArray

val filePathFromResourcesDirectory = "google-docs-key.p12"
val serviceAccountId = "[something]@drive-integration-[something]"
val googleSheetId = "1nC8Y3a8cvtXhhrpZCNAsP4MBHRm5Uee4xX-rCW3CW_4"
val tabName = "Favorite Cities"

import spark.implicits
val df = Seq(("Brooklyn", "New York"), 
          ("New York City", "New York"), 
          ("San Francisco", "California")).
          toDF("City", "State")

  option("serviceAccountId", serviceAccountId).
  option("credentialPath", resourceToLocal(filePathFromResourcesDirectory)).


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPradip KaradView Question on Stackoverflow
Solution 1 - ScalaAndreas NeumannView Answer on Stackoverflow
Solution 2 - ScalaMohsen KashiView Answer on Stackoverflow
Solution 3 - ScalafreedevView Answer on Stackoverflow
Solution 4 - ScalaSriView Answer on Stackoverflow
Solution 5 - ScalaDileep DominicView Answer on Stackoverflow
Solution 6 - ScalaTony FraserView Answer on Stackoverflow