Scala

画像ファイルをダウンロードして保存する

More than 3 years have passed since last update.
Downloader.scala
import java.io.{FileOutputStream, BufferedOutputStream}
import java.net.URL
import scala.language.postfixOps

object Main {
  def main(args: Array[String]) = {
    val image_url = "http://pic.prepics-cdn.com/fuualbum0408/19574639.jpeg"
    val file_name = "hato.jpg"
    download(image_url, file_name)
  }

  def download(url: String, file_name: String) = {
    val stream = new URL(url).openStream
    val buf = Stream.continually(stream.read).takeWhile( -1 != ).map(_.byteValue).toArray
    val bw = new BufferedOutputStream(new FileOutputStream(file_name))
    bw.write(buf)
    bw.close
  }
}