Updated April 20, 2023
Introduction to Scala Read File
Scala File I/O is an important concept of file handling in Scala. It comes up with all the native libraries and dependencies required for Reading of the File as we are operating it after further read. It reads it from various file formats like .txt, .csv and do operation after reading. Scala.io.Source class takes care of the methods for reading of a file and various operation associated with it.
Syntax:
To use the Scala Read File we need to have the Scala.io.Source imported that has the method to read the File.
Import scala.io.Source
Source.fromFile("Path of file").getLines // One line at a Time
Source.fromFile("Path of File").getLines.toList // File to List
Console.readline //used to read the File from the console only.
Reading Files in Scala with Example
We can read various files from Scala from the location in our local system and do operation over the File I/O.
Let us see some methods how to read files over Scala:
1. Reading Scala File from Console
We can read file from console and check for the data and do certain operations over there.
Example:
Console.readline method is used to read it from console.
Just write the line inside readline and it will read it from there.
Code:
scala> Console.readLine("It will read it from here")
warning: there was one deprecation warning; re-run with -deprecation for details
It will read it from here
Output:
2. Loading it from Input File
We can load data from file system in and do operations over the file. The Source.fromfile will load the data from a file and do operations over the file.
Example:
We have text file with the name of name of Demo.txt that we will load from in scala and read the data line one at a time.
Code:
scala> import scala.io.Source
import scala.io.Source
scala> Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").mkString
res10: String =
My name is Gaurav
My name is Agarwal
My name is Arpit
We are making a string using the mkstring method and print the value that it has.
Demo3.txt
Output:
But suppose if we want to process each line instead of whole files can be achieved with .getLines() function.
This methods reads the value line by line of a text file and does operation over that.
Example:
Over the same text file Demo3.txt if we use the method .getlines it will first an non – empty iterator of string data type and then traversing will print the elements aside.
Code:
scala>
Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").getLines()
res11: Iterator[String] = non-empty iterator
scala> Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").getLines().foreach{x =>println(x)}
My name is Gaurav
My name is Agarwal
My name is Arpit
Output:
The take() function take the line element we want to read it from that read file.
Example:
Code:
scala>
Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.take(1).foreach(println)
My name is Gaurav
scala> Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.take(2).foreach(println)
My name is Gaurav
My name is Agarwal
scala> Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.take(3).foreach(println)
My name is Gaurav
My name is Agarwal
My name is Arpit
So the take(1), (2), (3) will take the elements from the file and print that accordingly.
.slice method is also used to take the slice of the lines if we want the operation over a particular slice of lines within the file. This can be done using the slice function that takes the range from and until.
Example:
In the above example if we want to operate over two lines of a file we can use the .slice function as of:
Code:
scala>
Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.slice(0,2).foreach(println)
This takes up the two lines and gives the result for operation.
My name is Gaurav
My name is Agarwal
Output:
We can also change the file to List or to array after reading it by using the method .toList and .toArray over the code.
Example:
Code:
scala> Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").toList
res12: List[Char] =
,ist(M, y, , n, a, m, e, , i, s, , G, a, u, r, a, v, ,
, M, y, , n, a, m, e, , i, s, , A, g, a, r, w, a, l,
, M, y, , n, a, m, e, , i, s, , A, r, p, i, t)
Converts it to List
Converts it to Array
scala> Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").toArray
res13: Array[Char] =
,rray(M, y, , n, a, m, e, , i, s, , G, a, u, r, a, v, ,
, M, y, , n, a, m, e, , i, s, , A, g, a, r, w, a, l,
, M, y, , n, a, m, e, , i, s, , A, r, p, i, t)
Output:
Disposing or Closing of a file is also needed as it takes up the memory over the JVM. So .close method is use to close the file after the operation is done over the file. And even for automatically closing we can use the .dispose method by handling it within the file so that the required space is freed up for further operations.
Code:
Scala> val b = Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt")
b: scala.io.BufferedSource = non-empty iterator
So this buffered source has to be closed once the operations are done over them. So we use the .close method to perform the same.
scala>b.close
So from this we saw how can we can read a file in scala and use operations with it.
Conclusion
Here from the above article we saw how we can use the various method to read file in Scala. We also saw how the Scala.io.Source provides method to read files in scala and perform operation over them. With the help of various examples, we saw the different aspects and the advantages of using reading files using different method.
Recommended Articles
We hope that this EDUCBA information on “Scala Read File” was beneficial to you. You can view EDUCBA’s recommended articles for more information.