DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Self-Healing Directory: Architecting AI-Driven Security for Active Directory
  • Scaling Identity Governance Without Connectors: The LDAP Directory IGA Integration Pattern
  • Why Developers Should Pay Attention to Internal Directory Security
  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala

Trending

  • Architecting Proactive IT: NinjaOne Remote Monitoring and Management
  • Give Your AI Assistant Long-Term Memory With perag
  • WebSocket Debugging Without a Proxy — A Browser-First Workflow
  • Observability for Agents and Workflows: Tracing Prompts, Tool Calls, and Business Outcomes End-to-End
  1. DZone
  2. Coding
  3. Languages
  4. Working With Resources, Folders, and Files

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.

By 
Alexey Zvolinskiy user avatar
Alexey Zvolinskiy
·
Feb. 10, 16 · Analysis
Likes (4)
Comment
Save
Tweet
Share
23.8K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Scala (programming language) Directory

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

Opinions expressed by DZone contributors are their own.

Related

  • The Self-Healing Directory: Architecting AI-Driven Security for Active Directory
  • Scaling Identity Governance Without Connectors: The LDAP Directory IGA Integration Pattern
  • Why Developers Should Pay Attention to Internal Directory Security
  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook