How to use Java.io.PrintWriter Class In Scala

Writing a CSV File

Data will be written to a CSV file using the java.io.PrintWriter class. The data will be written row by row, with each field separated by a comma. The data will be in form of strings and first row will be the header of the data.

Functions:

  1. new PrintWriter(new File(filename)): Opens or creates a file for writing.
  2. writer.println(data): Writes a line of data to the file.

Below is the Scala program to write CSV File:

Scala
import java.io.PrintWriter
object WriterExample1 {
  def main(args: Array[String]): Unit = {
   val filename = "outputs.csv"
    val writer = new PrintWriter(filename)
    writer.println("Name, Age, City")
    writer.println("John, 30, New York")
    writer.println("Alice, 25, London")
    writer.close()
  }
}

Explanation:

  1. Import important libraries: Scalaā€™s java.io.PrintWriter class can be used to write to files.
  2. Create the CSV file: Use new PrintWriter(new File(ā€œoutput.csvā€)) to open or create a new CSV file.
  3. Write data to the file: Use println or write methods to write data to the file.

Output:

Write CSV File in Scala

Reading CSV In Scala

We will read a CSV file using scala.io.Source. The file will be read line by line, and each line will be sperate into fields using the comma.

Functions:

  1. Source.fromFile(filename): Opens the file for reading.
  2. file.getLines(): Reads the file line by line.
  3. line.split(delimiter): Splits each line into fields based on the delimiter.

Below is the Scala program to read a CSV File:

Scala
import scala.io.Source

object ReaderExample1 {
  def main(args: Array[String]): Unit = {
    val filename = "output.csv"
    val delimiter = ","
    val file = Source.fromFile(filename)
    for (line <- file.getLines()) {
        val fields = line.split(delimiter).map(_.trim)
        println(fields.mkString(", "))
    }
    file.close()
  }
}

Explanation:

  1. Import the required libraries: In Scala, you can use the scala.io.Source library to read files.
  2. Open CSV file: Use Source.fromFile(ā€œoutput.csvā€) to open the CSV file.
  3. Read the data: Split each line using the delimiter and process the data as needed.

Output:

Read CSV File in Scala

How to Read and Write CSV File in Scala?

Data processing and analysis in Scala mostly require dealing with CSV (Comma Separated Values) files. CSV is a simple file format used to store tabular data, such as a spreadsheet or database. Each line of a CSV file is plain text, representing a data row, with values separated by commas (,). Reading from and writing to CSV files are common tasks across several programming situations. This article focuses on discussing steps to read and write CSV files in Scala.

Table of Content

  • Setting Up the Environment
  • Using Java.io.PrintWriter Class
  • Using Scala-csv Library
  • Conclusion
  • FAQs

Similar Reads

Setting Up the Environment

To work with CSV files in Scala, you need to set up your development environment. Ensure you have Scala installed and a build tool like SBT (Scala Build Tool)....

Using Java.io.PrintWriter Class

Writing a CSV File...

Using Scala-csv Library

Writing CSV in Scala...

Conclusion

The scala-csv library is an efficient way of reading and writing CSV files in Scala. By following the steps given above, it is easy to add the operations on CSV file into your Scala applications. This feature is important in ETL processes, data science and other domains that involve data manipulation....

FAQs

1. How can I handle large CSV files in Scala?...