Scala File Input and Output

Overview of reading and writing file operations

File operations mostly include reading data from the file or writing data into files.

Writing Data from File
e.g.

import java.io._
object Intellipaat {
def main(args: Array[String]) {
val writer = new PrintWriter(new File("intellipaat.txt" ))
writer.write("Hello Intellipaat")
writer.close()
}
}

When we compile and execute this program then it writes Hello Intellipaat in intellipaat.txt file.

Certification in Bigdata Analytics

Reading Data from File
e.g.

import scala.io.Source
object Intellipaat {
def main(args: Array[String]) {
println("Data in File is:" )
Source.fromFile("intellipaat.txt" ).foreach{
print
}
}
}

Compile and execute the above program:
scalac Intellipaat.scala
scala Intellipaat
Output
Data in file is: Hello Intellipaat

Our Big Data Courses Duration and Fees

Program Name
Start Date
Fees
Cohort starts on 18th Jan 2025
₹22,743
Cohort starts on 25th Jan 2025
₹22,743
Cohort starts on 18th Jan 2025
₹22,743

About the Author

Technical Research Analyst - Big Data Engineering

Abhijit is a Technical Research Analyst specialising in Big Data and Azure Data Engineering. He has 4+ years of experience in the Big data domain and provides consultancy services to several Fortune 500 companies. His expertise includes breaking down highly technical concepts into easy-to-understand content.