File input/output: Difference between revisions

Content deleted Content added
→‎{{header|Scala}}: Exception-handling added
Line 1,877: Line 1,877:
[[Category:Scala Implementations]]
[[Category:Scala Implementations]]
{{libheader|Scala}}
{{libheader|Scala}}
<lang scala>import java.io.PrintWriter
<lang scala>import java.io.{ FileNotFoundException, PrintWriter }


object FileIO extends App {
object FileIO extends App {
try {
val MyFileTxtSource = scala.io.Source.fromFile("input.txt")
val MyFileTxtTarget = new PrintWriter("output.txt")


val str = MyFileTxtSource.mkString
val MyFileTxtSource = scala.io.Source.fromFile("input.txt")
val MyFileTxtTarget = new PrintWriter("output.txt")
MyFileTxtTarget.print(str)


MyFileTxtTarget.close()
val str = MyFileTxtSource.mkString
MyFileTxtSource.close()
MyFileTxtTarget.print(str)
}

} catch {
MyFileTxtTarget.close()
case e: FileNotFoundException => println(e.getLocalizedMessage())
MyFileTxtSource.close()
}
}</lang>
}</lang>