Scala Write to File

There is a time when we need to put information into a file, and at the same time, we want to retrieve data from that file. But Scala doesn’t have any feature of writing to file. We have to import the package from the Java library to write in a file.

What is PrintWriter in Scala:

To write a file in Scala, we have to import We use printWriter to pass our object inside it. Then, we can write in a file after using a PrintWriter object inside our program. The includes all the printing methods that are in printstream.

Syntax to write a file in Scala:
The general syntax of Scala write to file is given below:

val variable_name = new PrintWriter("file_name")
variable_name.write("Text to write in file")

The syntax is very clean and concise for writing in a file. First, we have to define a variable that initializes the object for the printWriter class, and then we can pass our file objects in it. After creating the object, we can call the write method, which allows us to write in a file.

Writing in a file using Scala in Ubuntu 20.04:

To understand the file creation and to write in Scala, we should take a look at the examples:

Example # 1: Creating a file by importing java package in Scala

We create a new file first and then write it into the newly created file. Below, we have an implementation code to create a new file and use PrintWriter from the package to write in the file.

In an example, first, we need to import a java package that will allow us to create files in Scala. As we have imported Java.up.File and in the program. Now, create a class “File1” which defines the main method of the class. In the main function, we have made a file as “scala.txt” which is stored in a variable named “myFile1”. Then we have a new variable as “myFile2” in which we have passed the reference of the file to PrintWriter. Now, we can write in a file by calling the write method. We have added some text in the file, and after writing into a file, we have closed printWriter by the close method.

As you can see, a text file is created, and the text “welcome to Scala language” is stored in a file that we write using the PrinterWriter.write method.

Example # 2: Reading from a file in Scala by using the getline method:

As we cannot read line by line at once. So the Scala source package has a getline method available in it. The getline method allows us to read line by line. Let’s have an example program to use the getLine method in a file.

In the example code, we have to import and, which will allow us to write a file and give access to the getline method. First, we have declared a class with the name “File2”. In the main method of class “File2”, we have mentioned file name in a variable “file_name” from which we need to read in a file. We read the file “scala.txt” that we have created in Example 1. As we have mentioned the file name, now we have passed the file object into a new variable, “file_source” that will read from the program itself through a source file. Use the getline method to print the text line by line in the loop. The print statement will print the text line from the file. After reading or writing a file, we have a close file reference by using the close method.

Upon execution and implementation of the code above, we can read a text line from a file “scala.txt” by using the getLine method. The text lines are shown on the Ubuntu 20.04 terminal.

Example # 3: Reading each character using the FromFile method into a file in Scala:

The scale provides the class to read the file from the class source. To read the text in a file, we call the “fromfile” method of the class source. Then, we can read the file content, which has the file name as an argument. Below is the implementation of code for reading each character from a file.

In the above example of code implementation, we have imported that will give access to read in a file. Then create a class “File3” and define the main method in the class. We have a text file “scala.txt” created in a previous example. The text file is stored in a variable “file_name” then passes a source.fromfile in a variable represented as “file-source” which generates a source from the input file. Using a while loop for the source file creates an iterable representation. The file” hasNext” in a while loop verifies if there are the next elements available to iterate. In the end, closing the file with a close method from external use.

We have fetched each character as an output from a file, as displayed in the image below.


Scala does not provide a writing class but supports reading in a file. We can say Scala has file handling features. To work with file handling, we have to create a file first, then we can write in a file, and at the end, we can read from a file with a different method of file reading. We have mentioned above all the methods from which we can read lines of text through the getLine method. If we want to read character by character, we have the fromFile method, which returns each character from the text lines. All the methods have been discussed with the code implementation.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.