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

Scala: Work With Files and Directories

DZone's Guide to

Scala: Work With Files and Directories

In this post, we take a look at how to deal with files and directories in Scala. Read on to find out more and for some examples.

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

How often do you need to write or read files? This is a pretty common task in any more or less non-trivial project. Image upload process, CSV or XML parsing, XLS report generation etc. All these operations imply input or output processing. So I’m going to make an overview of the most powerful library for working with files in Scala.

Here is a list of operations which I want to cover:

  • File creation and removal
  • File writing and reading
  • File comparison
  • Zipping and unzipping

Of course, this list doesn’t limit the number of useful functions that you can perform with files and directories.

The most suitable format for this article is tutorial plus example, so I’ll try to type more lines of code and less blah, blah, blah. If you want to discuss something, feel free to leave a comment.

Looking a little ahead, I want to say that I will not write about native scala.io.Source. Instead, I’ll show a powerful library called better-files. The lib is written on top of Java NIO and has very transparent API.

Before we start, we need to add the library to a project. Here is a simple way to do this with SBT:

libraryDependencies += "com.github.pathikrit" %% "better-files" % "2.16.0"

Keep in mind that you need to use an appropriate version. At the moment of writing, 2.16.0 is the latest version.

The next step is to add two imports:

import better.files._
import better.files.File._

Finally, we can start manipulations with files.

How to Create a File or Directory (Or Handle If It Doesn't Exist)

val file1: File = "/Users/Alex/Downloads/file1.txt"
  .toFile
  .createIfNotExists()

It’s so simple, isn’t it? After this code is executed, file1.txt is created by the following path /Users/Alex/Downloads/.

Look at the createIfNotExists() method. You can pass parameters into it. For example, you want to create a directory.

val dir: File = "/Users/Alex/Downloads/dir"
  .toFile
  .createIfNotExists(true)

Passing true into the method, we force a dir folder creation by the path /Users/Alex/Downloads/.

What about the creation of a folder with an extra path? Let’s assume that there are no directories in the /Users/Alex/Downloads/.

val dir: File = "/Users/Alex/Downloads/extra/path/dir"
  .toFile
  .createIfNotExists(true, true)

The code snippet above creates three folders: extra, path, and dir.

The same approach can be applied to the file creation. If you want create a parent folder(-s) for the file, simply set boolean value as the createParents parameter.

How to Write to a File

The most popular question which you may find on StackOverflow. At least I think so. With help of better-files you can write to files easily!

val html = (root/"Users/Alex/Downloads"/"test.html")
  .createIfNotExists()
  .append("<h1>")
  .appendLines("", "Header", "</h1>")

As you see, two options are allowed: write one line or write multiple lines. Also, you can write directly bytes in a file.

Another useful input operation is an overwriting. In the context of the previous example, we can see how to write entirely new content to a file:

html.overwrite("<p>Just one paragraph</p>")

How to Read a File

The most robust way to read all lines of text from a file is:

val simpleFile = (root/"Users/Alex/Downloads"/"simple_file.txt")
  .append("simple text")

//simple text
println(simpleFile.contentAsString)

Alright! Getting an entire content as a string is pretty simple. But what if we want to traverse through a content line by line? It’s also possible:

val simpleFile = (root/"Users/Alex/Downloads"/"simple_file.txt")
  .overwrite("")
  .appendLines("#1 line", "#2 line", "#3 line")

simpleFile.lines.map(line => println(s"decor $line decor"))

Voila!

As I mentioned in the file writing section, you may want to work with bytes. Here is a small example of such an approach:

val simpleFile = (root/"Users/Alex/Downloads"/"simple_file.txt")
    .overwrite("I'm from simple_file.txt")

val destinationFile = (root/"Users/Alex/Downloads"/"test.txt")
    .write(simpleFile.byteArray)(OpenOptions.default)

println(destinationFile.contentAsString)

Delete Files and Directories

Another group of popular questions is about the delete operation. Let’s consider the most trivial example, when we need to delete a file when it exists:

val forDelete = (root/"Users/Alex/Downloads"/"file_to_delete.txt")
  .createIfNotExists()

if (forDelete.exists)
  forDelete.delete()

The same method can be applied to a process of directory deletion. Furthermore, it runs recursively and deletes all files and directories inside of a target folder.

How to Compare Files and Folders

It’s a pretty common scenario for developers. What does the better-files library suggest for solving this problem? Use == when you want to compare two files or directories by path. Use === when you want to check equality of two files or folders by content. The comparison of two folders' content has never been easier.

How to Zip and Unzip Files and Directories

In order to zip file or directory, just use following construction:

val forZip = (root/"Users/Alex/Downloads"/"gameboard.xlsx")
forZip.zipTo(root/"Users/Alex/Downloads"/"archive.zip")

When you need to unzip an archive, simply call the unzipTo method.

Summary

I never thought that work with files may as so easy as it is with better-files. In this article, I showed the most popular operations, but with this library you can do much more. For example, you can copy the file or directory to some destination, work with file attributes (extension, size), set permissions, scan content. Now you understand why this library works with files like a boss!

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
scala ,java ,libraries ,better-files

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}