Double Delight Sale: Flat 70% + Addl. 30% Off + 30-days Extra Lab on all Courses | Use Coupon DS30 in Checkout | Offer Expires In

  Enroll Now

Scala

47 / 52
   

Scala - Assessment - Write to a file

In Scala, reading from and writing to files on file-system is very easy. After importing java.io._, you can use an instance of PrintWriter to write to a file.

For example, to write string "Hello" to a file named test.txt, you can use below code:

val writer = new PrintWriter(new File("test.txt" ))
writer.write("Hello")
writer.close()

Now, let's write a Scala program which will write below 2 phrases in 2 lines in a file named "New.txt".

Hello Scala
Bye Scala

To write the second line into a new line, write a new line character to the file after the first line.

New line character is indicated by "\n". so, you will write "Hello Scala", then "\n" followed by "Bye Scala" to the file.

INSTRUCTIONS
  • Switch to Jupyter tab.
  • Don't forget to import java.io._
  • Write the code to do the following:

    • Open a PrintWriter to a file with a name "New.txt"
    • Write "Hello Scala" into this printwriter
    • Write "Bye Scala" into this printwriter
    • Close the printwriter
  • Press Shift+Enter.

  • Submit your answer.

No hints are availble for this assesment

Answer is not availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...