

Scala> val b = omFile("C://Users//arpianan//Desktop//Demo3.txt")ī: scala.io.BufferedSource = non-empty iterator dispose method by handling it within the file so that the required space is freed up for further operations. And even for automatically closing we can use the. close method is use to close the file after the operation is done over the file. ,rray(M, y,, n, a, m, e,, i, s,, G, a, u, r, a, v, ,ĭisposing or Closing of a file is also needed as it takes up the memory over the JVM. Scala> omFile("C://Users//arpianan//Desktop//Demo3.txt").toArray Scala> omFile("C://Users//arpianan//Desktop//Demo3.txt").toList

We can also change the file to List or to array after reading it by using the method. This takes up the two lines and gives the result for operation. omFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.slice(0,2).foreach(println) In the above example if we want to operate over two lines of a file we can use the. This can be done using the slice function that takes the range from and until. 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. So the take(1), (2), (3) will take the elements from the file and print that accordingly. Scala> omFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.take(3).foreach(println) Scala> omFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.take(2).foreach(println) omFile("C://Users//arpianan//Desktop//Demo3.txt").getLines.take(1).foreach(println) The take() function take the line element we want to read it from that read file. Scala> omFile("C://Users//arpianan//Desktop//Demo3.txt").getLines().foreach omFile("C://Users//arpianan//Desktop//Demo3.txt").getLines() getlines it will first an non – empty iterator of string data type and then traversing will print the elements aside. Over the same text file Demo3.txt if we use the method. This methods reads the value line by line of a text file and does operation over that.

We are making a string using the mkstring method and print the value that it has.īut suppose if we want to process each line instead of whole files can be achieved with. Scala> omFile("C://Users//arpianan//Desktop//Demo3.txt").mkString 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. The omfile will load the data from a file and do operations over the file. We can load data from file system in and do operations over the file. Warning: there was one deprecation warning re-run with -deprecation for details Scala> Console.readLine("It will read it from here") Just write the line inside readline and it will read it from there. Console.readline method is used to read it from console.
