Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Working With Resources, Folders, and Files

DZone's Guide to

Working With Resources, Folders, and Files

Learn about working with files, folders, and resources in Scala projects, such as accessing the resources folder, and reading files line by line.

Free Resource

Migrating from On-Prem to Cloud Middleware? Here’s what Aberdeen Group says leading companies should be considering. Brought to you in partnershp with Liaison Technologies

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 the file system, Scala isn’t an exception. So how it can be done in practice?

I’m going to demonstrate a short example on a real Scala project with 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 Files 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!

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
resources ,scala ,files

Published at DZone with permission of Alexey Zvolinskiy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}