Code:
import java.io.PrintWriter
import scala.io.Source
object ScalaExample {
def main(args:Array[String]):Unit={
val writer=new PrintWriter("C:\\Users\\Nethra\\Desktop\\test.txt")
writer.write("Just some Random Text\n Some more Random Text")
writer.close()
val textFromFile=Source.fromFile("C:\\Users\\Nethra\\Desktop\\test.txt")
val lineIterator=textFromFile.getLines()
for(line<-lineIterator)
print(line)
textFromFile.close()
}
import java.io.PrintWriter
import scala.io.Source
object ScalaExample {
def main(args:Array[String]):Unit={
val writer=new PrintWriter("C:\\Users\\Nethra\\Desktop\\test.txt")
writer.write("Just some Random Text\n Some more Random Text")
writer.close()
val textFromFile=Source.fromFile("C:\\Users\\Nethra\\Desktop\\test.txt")
val lineIterator=textFromFile.getLines()
for(line<-lineIterator)
print(line)
textFromFile.close()
}
}
Output:
ScalaExample
Just some Random Text
Some more Random Text
Process finished with exit code 0
TRY and CATCH operations:
scala> def d(n1:Int,n2:Int)=try
| {
| (n1/n2)
| }catch{
| case ex:java.lang.ArithmeticException=>"can't divide by zero"
| }finally{
| //cleanup after exeption
| }
d: (n1: Int, n2: Int)Any
scala> println("3/0=",d(3,0))
(3/0=,can't divide by zero)
No comments:
Post a Comment