Reading and Writing Files

Now that you know how to get user input in Scala, it’s time to see how to write data to a file. We can use the java.io.File object to achieve this. Here’s an example of writing to a file:

UsingScala/WriteToFile.scala
 
import​ java.io._
 
 
val​ writer = ​new​ PrintWriter(​new​ File(​"symbols.txt"​))
 
writer write ​"AAPL"
 
writer.close()

This simple code writes the symbol “AAPL” to the file named symbols.txt.

Reading files is really simple as well. Scala’s Source class and its companion object come in handy for this purpose. For illustration purposes, let’s write a Scala script that reads itself:

UsingScala/ReadingFile.scala
 
import​ scala.io.Source
 
 
println(​"*** The content of the file you read is:"​)
 
Source.fromFile(​"ReadingFile.scala"​).foreach { print }

We read the file that contains this code and printed out its contents. As you know, reading a file is not such a simple task in Java, with all the try-catch code that gets in the way. The output from the code is shown here:

 
*** The content of the file you read is:
 
import scala.io.Source
 
 
println("*** The content of the file you read is:")
 
Source.fromFile("ReadingFile.scala").foreach { print }

The Source class is an Iterator over the input stream. The Source companion object has several convenience methods to read from a file, an input stream, a string, or even a URL, as you’ll see soon. The foreach method helps you get one character at a time—the input is buffered, so no worries about performance. If you’re interested in reading a line at a time, you’d use the getLines method instead.

Very soon we’ll need to read information off the web. While discussing Source, let’s take a look at its fromURL method. This method is useful for reading the content of a website, a web service, or just about anything that you can point at using a URL. Here’s an example that reads the content from the Apache web server running locally on my machine, the localhost:

UsingScala/ReadingURL.scala
 
import​ scala.io.Source
 
import​ java.net.URL
 
 
val​ source = Source.fromURL(​new​ URL(​"http://localhost"​))
 
 
println(s​"What's Source?: ${source}"​)
 
println(s​"Raw String: ${source.mkString}"​)

Here’s the output from the program:

 
What's Source?: non-empty iterator
 
Raw String: <html><body><h1>It works!</h1></body></html>

We called fromURL to obtain a Source instance from that URL. The Source is an iterator that can be used to traverse through the contents. We can use methods of Source, like getLines, to process one line at a time. Alternately, we can concatenate all the lines into one String using the mkString method.

Although the previous example may quench your thirst to read and write files and access a URL, we need to get back to the net asset application. We could store the ticker symbols and units as plain text. Reading the file is easy, but then parsing through the contents of the file to get various ticker symbols and units isn’t going to be that easy. As much as we all hate XML for its verbosity, it does come in handy to organize this kind of information and parse it. Let’s make use of XML for the net asset application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.143.65