File Handling in Scala

File Handling is a way to store the fetched information in a file. Scala provides packages from which we can create, open, read and write the files. For writing to a file in scala we borrow java.io._ from Java because we don’t have a class to write into a file, in the Scala standard library. We could also import java.io.File and java.io.PrintWriter

Create a New File

  • java.io.File defines classes and interfaces for the JVM access files,file system and attributes.
  • File(String pathname) converts the parameter string to abstract path name create a new file instance.
Writing to File
  • java.io.PrintWriter includes all the printing methods include in PrintStream.

import java.io.File 
import java.io.PrintWriter 


object test13 {
  
  def main(args : Array[String]){
  
            // Creating a file  
        val file_Object = new File("C:/Users/AF62752/Desktop/abc.txt" )  
   
        // Passing reference of file to the printwriter      
        val print_Writer = new PrintWriter(file_Object)  
  
        // Writing to the file        
        print_Writer.write("Hi Welcome, Thanks for Learing File Handling ")  
  
        // Closing printwriter     
        print_Writer.close()   
    
}
}




Reading a File
scala.io.Source includes methods for iterable representation of the source file.
Source.fromFile creates a source from the input file.
file.next return the next element in the iteration and moves the iterator one step ahead.
file.hasnext checks if there is next element available to iterate.
getLines– Iterate through file line by line

object test13 {
 
  def main(args : Array[String]){
 
    val filename = "C:/Users/AF62752/Desktop/ddl.txt"   
    for (line <- Source.fromFile(filename).getLines) {
  println(line)
}
}
}


How Do I Append To The List?

In scala to append into a list, use “:+” single value

       var myList = List.empty[String]
       myList :+= "a"
       myList :+= "b"
       myList :+= "c"
       use++ for appending a list
       var myList = List.empty[String]
       myList ++= List("a", "b", "c")

What Are The Differences Between Array And Arraybuffer In Scala?

Differences between Array and ArrayBuffer in Scala:

 Array is fixed size array. We cannot change its size once its created.
 ArrayBuffer is variable size array. It can increase or decrease it’s size dynamically.
 Array is something similar to Java’s primitive arrays.
 ArrayBuffer is something similar to Java’s ArrayList.

 What is ofDim in Scala?

ofDim() is a method in Scala that lets us create multidimensional arrays. Since these let us store data in more than one dimension, we can store data like in a matrix. Let’s take an example.

    scala> import Array.ofDim
    import Array.ofDim
    scala> var a=ofDim[Int](3,3)
    a: Array[Array[Int]] = Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0))
    scala> var k=1
    k: Int = 1
    scala> for(i<-0 to 2){
        | for(j<-0 to 2){
        | a(i)(j)={i+k}
        | k+=1
        | }
        | k-=1
        | }
    scala> a 

What is a BitSet?

A bitset is a set of non-negative integers depicted as arrays. These arrays are variable in size and packed into 64-bit words. The largest number in a bitset determines its memory footprint. Let’s take an example.

    scala> import scala.collection.immutable._
    import scala.collection.immutable._
    scala> var nums=BitSet(7,2,4,3,1)
    nums: scala.collection.immutable.BitSet = BitSet(1, 2, 3, 4, 7)
    scala> nums+=9  //Adding an element
    scala> nums

res14: scala.collection.immutable.BitSet = BitSet(1, 2, 3, 4, 7, 9) 

What is a vector in Scala?

A vector is a general-purpose data structure that is immutable. We can use it when we want to hold a huge number of elements and want random access to them. This data structure extends the trait IndexedSeq and the abstract class AbstractSeq.

    scala> import scala.collection.immutable._
    import scala.collection.immutable._
    scala> var v1=Vector.empty
    v1: scala.collection.immutable.Vector[Nothing] = Vector()
    scala> var v2=Vector(7,2,4,3,1)
    v2: scala.collection.immutable.Vector[Int] = Vector(7, 2, 4, 3, 1)
    scala> var v3:Vector[Int]=Vector(8,2,6,5,9)
    v3: scala.collection.immutable.Vector[Int] = Vector(8, 2, 6, 5, 9)
    scala> v3=v3 :+7  //Adding a new element
    v3: scala.collection.immutable.Vector[Int] = Vector(8, 2, 6, 5, 9, 7)
    scala> v2++v3  //Merging two vectors

res19: scala.collection.immutable.Vector[Int] = Vector(7, 2, 4, 3, 1, 8, 2, 6, 5, 9, 7)

    scala> v3.reverse  //Reversing a vector

res20: scala.collection.immutable.Vector[Int] = Vector(7, 9, 5, 6, 2, 8)




No comments:

Post a Comment