Scala – File I/O

  • date 28th April, 2021 |
  • by Prwatech |
  • 0 Comments

File Handling is an approach to store the fetch data in a file. Scala provides packages from which we can read, open, create and compose the records. To write something in scala we get java.io._ from Java since we don’t have a class to write into a file, in the Scala standard library. We could likewise import java.io.File and java.io.PrintWriter.

Open, Read and Write a File in Scala

use case 1:

scala> import java.io._

import java.io._

scala> val writer=new PrintWriter(new File(“demo1.txt”))

scala> writer.write(“This is a Prwatech”)

scala> writer.close()

scala> import scala.io.Source

scala> Source.fromFile(“demo1.txt”).mkString

use case 2:

scala> import java.io._

import java.io._

scala> val writer=new PrintWriter(new File(“Prwatech.txt”))

scala> writer.write(“Big Data “)

scala> writer.write(” Data Science”)

scala> writer.write(” Tableau”)

scala> writer.close()

scala> import scala.io.Source

scala> Source.fromFile(“Prwatech.txt”).mkString

Quick Support

image image