Ways of Reading a File in Scala
There are three different ways of reading a file in the Scala programming language in Ubuntu 20.04, which we are going to discuss below:
The Sample Text File for Reading
We will be using the same text file for reading the three examples provided. We have created this sample text file within the Home Directory of our system and have named it “Sample.txt”. The contents of this text file are shown in the following image. However, it is not compulsory to use only a text file. You can also use files belonging to any other file format for reading, and you can modify their contents accordingly.
Method # 1: Reading a File in Scala Without Closing It
In this method, the technique that we will use for reading a file in Scala will open a file for reading and read the contents of that file. However, it will leave the said file open. This method is relatively simple. But the only drawback is that it does not close the file after performing the desired operations. The Scala script reading a file through this technique is shown in the image below:
In this Scala script, we have first imported the “scala.io.Source” library that implements the function we will use to read the target file. Then, in our driver function, we have defined a value named “filename”. We have assigned the complete path of our file to be read to this value. Then, we have used a “for” loop, which iterates through all the lines of the target text file by using the “line” iterator. We have printed the contents of this iterator within this loop and the contents of our text file on the terminal.
To compile this Scala code, the following command was used:
Then, for executing this Scala code, the command shown below was used:
This technique has correctly read all the contents of our specified sample text file, as you can witness from the output shown in the following image:
Method # 2: Reading a File in Scala and Properly Closing It
This is a relatively lengthy method of reading a file in Scala. However, this technique will open a file and close it properly after reading its contents. Nevertheless, you will have to close the target file manually by using this approach. The Scala code shown below demonstrates this:
Again, we have imported the same library that we used in the Scala script previously provided. In this code, we have defined a value named “bufferedSource”. After that, we have assigned the complete path of our sample text file to this value. However, we have enclosed this path within the “Source.fromFile” notation. Then, while using the “for” loop for iterating over the contents of this file, we have used the “bufferedSource.getLines” notation. Within this loop, we have printed all the lines on the terminal. Outside this loop, we used the “bufferedSource.close” statement to close our sample text file. Finally, to confirm that our sample text file has been closed successfully, we have published a confirmation message on the terminal.
When we executed our script, the following output appeared on the terminal. This approach also managed to print the contents of our file on the terminal correctly. Additionally, this script also notifies the user that the sample text file has been closed successfully.
Method # 3: Reading a File in Scala in Java Style
If you are fond of using Java and have recently switched to Scala, you might like to do everything in Java style. This method is also based on Java’s way of reading a file, and you can learn it by using the Scala script shown below:
In this Scala code, we have imported two different libraries of Java that will help us read a file. Then, we have defined a value named “file” and have created a new “File” instance to which we have passed the complete path of our sample text file. After that, we have defined another value named “reader”, created a new “Scanner” instance, and have passed to it the “file” value. Then, we have used a “while” loop that will iterate till the “reader” variable has something to read. Within this loop, we have defined a value “line” and have assigned to it the result of the “reader.nextLine()” function. Finally, we wanted to print all the lines of our file on the terminal.
This technique reads the contents of our sample text file, as shown in the following image:
This article was based on the concept of file reading in the Scala programming language in Ubuntu 20.04. We wanted to share the three different methods used most commonly in Scala to read a file. The first method simply opens a file, reads it, and then leaves it as it is without closing it. This is the simplest method of file reading in Scala. On the other hand, the second method is quite lengthy, but it properly closes a file after processing it.
In contrast, the last method depicted the Java’s style of reading a file. You can use any of these methods for reading a file in the Scala programming language according to your choice. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.