Scala

How to Remove Header and Trailer of File using Scala

Removing header and trailer of the File using Scala might not be real-time use case since you will be using Spark when dealing with large datasets. This post helpful mainly for interview purpose, An Interviewer might ask to write code for this using scala instead Unix/Spark.

Here is the code snippet to achieve the same using Scala –

import scala.io.Source
 
 
object RemoveHeaderTrailer{
 
def main(args: Array[String]){
println("start")
val input = Source.fromFile("C:/Users/Sai/input.txt")
//input.getLines().drop(1).foreach(println)//This is for removing Header alone

val lines = input.getLines().toList
val required_data = lines.slice(1,lines.size-1).mkString("\n")
import java.io._
val pw = new PrintWriter(new File("C:/Users/Sai/output.txt"))
pw.write(required_data)
pw.close()
println("end")
}
}

Share This Post

Avatar
An Ambivert, music lover, enthusiast, artist, designer, coder, gamer, content writer. He is Professional Software Developer with hands-on experience in Spark, Kafka, Scala, Python, Hadoop, Hive, Sqoop, Pig, php, html,css. Know more about him at www.saikumar.me

Lost Password

Register