Scala fetch string from URI and save results in file.

Scala example of fetching a string from remote URI, saving results in local file using command line parameters.
would normally use Curl for this.

See Also: Useful Scala Resource Links

Demonstrates

  • Reading command line parameters
  • Display error and correct sample when insufficient command line parameters.
  • Retrieving text from a remote URI using Source.FromURL
  • Specifying encoding for remote server when fetching from URL
  • Saving the retrieved string to a file
  • Detecting Error on download and display error message.

Use From Shell

   Compile
      scalac HTTPGet.scala
   Run
      scala HTTPGet SLV.HTML  "http://finance.yahoo.com/quote/SLV/?p=SLV"

 

HTTPGet.scala

/* HTTPGet.scala
 *   Example of fetching a string from remote URI
 *   and saving in local file.  Reads URI and 
 *   file save file name from command line.
 *   would normally use Curl for this.   
 */
 
import scala.io.Source
 
object HTTPGet {
  def main(args: Array[String]){
     System.out.printf("HTTPGet.scala")     
     if (args.length != 2) {
        println("HTTPGet.scala usage=   scala HTTPGet SaveFileName  URI");
        println("""example   scala HTTPGet SLV.HTML  "http://finance.yahoo.com/quote/SLV/?p=SLV" """);
     } else {
       val outFiName = args(0);
       val uri = args(1);             
       println("outFiName=" + outFiName);
       println("uri=" + uri);
       try{
           val html = Source.fromURL(uri)("ISO-8859-1")
           val s = html.mkString
           try {
             scala.tools.nsc.io.File(outFiName).writeAll(s)
           } catch {
             case e: Exception => println("fail write file " + outFiName + " e=" + e);
           }          
       } catch {
           case e: Exception => println("fail fetch URI: e=" + e);
       } 
     }
  }
}

We offer expert consulting services

We offer consulting services with deep expertise in Search, Machine Learning, Distributed Architecture and High performance sites.   We specialize in solving the hardest problems you have available. Please contact us if you need engineering help.  joeeatbayesanalyticphone number

One Reply to “Scala fetch string from URI and save results in file.”

Leave a Reply