Read Each Line and Put in an Array Scala
Introduction to Scala Read File
Scala File I/O is an of import concept of file treatment in Scala. Information technology 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 afterwards reading. Scala.io.Source class takes intendance of the methods for reading of a file and diverse operation associated with information technology.
Syntax:
To use the Scala Read File we demand 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 Fourth dimension
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 Instance
We can read various files from Scala from the location in our local system and practise operation over the File I/O.
Let us run into some methods how to read files over Scala:
1. Reading Scala File from Console
We can read file from console and cheque for the data and practice certain operations over there.
Example:
Console.readline method is used to read it from console.
But write the line inside readline and it will read it from there.
Code:
scala> Console.readLine("It will read information technology from here")
warning: there was one deprecation alarm; re-run with -deprecation for details
It will read information technology from hither
Output:
two. Loading it from Input File
We can load data from file organisation in and do operations over the file. The Source.fromfile volition load the data from a file and practice operations over the file.
Example:
We accept text file with the name of name of Demo.txt that nosotros volition load from in scala and read the information line ane at a time.
Lawmaking:
scala> import scala.io.Source
import scala.io.Source
scala> Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").mkString
res10: Cord =
My name is Gaurav
My proper noun is Agarwal
My name is Arpit
We are making a cord 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 accomplished with .getLines() role.
This methods reads the value line past line of a text file and does operation over that.
Example:
Over the same text file Demo3.txt if nosotros utilise the method .getlines information technology volition showtime an not – empty iterator of string data type and 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(10)}
My proper name is Gaurav
My proper 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:
Lawmaking:
scala>
Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.have(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 proper noun is Agarwal
scala> Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.take(3).foreach(println)
My proper noun is Gaurav
My proper noun is Agarwal
My name is Arpit
So the take(ane), (two), (three) will take the elements from the file and print that appropriately.
.slice method is likewise used to take the piece of the lines if we want the operation over a particular slice of lines within the file. This can exist washed using the slice function that takes the range from and until.
Case:
In the above example if we want to operate over 2 lines of a file we can use the .slice part as of:
Code:
scala>
Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.slice(0,2).foreach(println)
This takes upwardly the 2 lines and gives the result for operation.
My proper name is Gaurav
My name is Agarwal
Output:
We can also change the file to List or to array after reading information technology 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(G, y, , north, a, m, eastward, , i, s, , M, a, u, r, a, v, ,
, M, y, , northward, a, m, due east, , i, s, , A, g, a, r, w, a, l,
, Chiliad, y, , due north, a, m, east, , 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(Yard, y, , n, a, yard, e, , i, s, , G, a, u, r, a, v, ,
, Chiliad, y, , n, a, g, e, , i, s, , A, thou, a, r, w, a, fifty,
, M, y, , northward, a, one thousand, e, , i, s, , A, r, p, i, t)
Output:
Disposing or Closing of a file is also needed every bit it takes up the memory over the JVM. And then .close method is use to close the file later the performance is done over the file. And even for automatically endmost we can employ the .dispose method by handling it within the file so that the required space is freed upwards for further operations.
Code:
Scala> val b = Source.fromFile("C://Users//arpianan//Desktop//Demo3.txt")
b: scala.io.BufferedSource = non-empty iterator
Then this buffered source has to exist closed once the operations are done over them. So we employ the .close method to perform the same.
scala>b.close
And so from this we saw how can we can read a file in scala and utilize operations with information technology.
Conclusion
Here from the above commodity we saw how we tin 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 dissimilar aspects and the advantages of using reading files using different method.
Recommended Manufactures
This is a guide to Scala Read File. Hither we discuss the introduction to Scala Read File, how to read files with example respectively. You lot may likewise have a look at the following manufactures to learn more –
- Scala Ready
- Scala Singleton
- Scala flatMap
- Scala Try Take hold of
Source: https://www.educba.com/scala-read-file/
0 Response to "Read Each Line and Put in an Array Scala"
Post a Comment