Scala 09 操作外部数据

1 本地文件、网络

package com.lihaogn.MyFile

import scala.io.Source

object FileApp {

  def main(args: Array[String]): Unit = {

    val file=Source.fromFile("/Users/Mac/testdata/hello.txt")(scala.io.Codec.UTF8)

    def readLine(): Unit ={
      for (line <- file.getLines()) {
        println(line)
      }
    }

//    readLine()

    def readChar(): Unit ={
      for (ele <- file) {
        print(ele+" ")
      }
    }

//    readChar()

    def readNet(): Unit ={
      val file=Source.fromURL("http://www.baidu.com")
      for (line <- file.getLines()) {
        println(line)
      }
    }

    readNet()
  }

}

2 MySQL数据

1)引入依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>

2)MysqlApp.scala

package com.lihaogn.MyFile

import java.sql.{Connection, DriverManager}

object MysqlApp {

  def main(args: Array[String]): Unit = {

//    val driver="com.mysql.jdbc.Driver"
    val url="jdbc:mysql://localhost:3306/my_db"
    val username="root"
    val password="rootroot"

    var connection:Connection=null
    try{

      // make the connection
      classOf[com.mysql.jdbc.Driver]
      connection=DriverManager.getConnection(url,username,password)

      val statement=connection.createStatement()
      val sql="select title,date from tb1"
      val resultSet=statement.executeQuery(sql)

      while (resultSet.next) {
        val title=resultSet.getString("title")
        val date=resultSet.getString("date")

        println(s"$title, $date")
      }

    }catch {
      case e:Exception=>e.printStackTrace()
    }finally {
      if(connection!=null) {
        connection.close()
      }
    }

  }

}

3 xml数据

1)添加依赖

<!-- scala xml -->
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-xml</artifactId>
      <version>2.11.0-M4</version>
    </dependency>

2)test.xml

<symbols>
    <symbol ticker="apple">
        <units>200</units>
    </symbol>
    <units>500</units>
    <symbol ticker="thinkpad">
        <units>
            400
        </units>
    </symbol>
</symbols>

3)XMLApp.scala

package com.lihaogn.scala

import scala.xml.XML

import java.io.{FileInputStream, InputStreamReader}

object XMLApp {

  def main(args: Array[String]): Unit = {

    loadXML()

    println("=======================")

    readXMLAttr()
  }

  def readXMLAttr(): Unit = {
    val xml = XML.load(this.getClass.getClassLoader.getResource("test.xml"))

    // 找symbol下的units
    val unit = xml \ "symbol" \ "units"

    println(unit)

    println("----------------------")

    // 找所有的units
    val fields = xml \\ "units"
    for (field <- fields) {
      println(field)
    }

    println("----------------------")

    // 找tiker属性
    // 方式一
    //    val fieldAttrs = (xml \ "symbol").map(_ \ "@ticker")
    // 方式二
    val fieldAttrs = (xml \ "symbol" \\ "@ticker")
    for (fieldAttr <- fieldAttrs) {
      println(fieldAttr)
    }

    println("----------------------")

    // 过滤出指定ticker的symbol
    // 方式一
    //    val filters = (xml \\ "symbol").filter(_.attribute("ticker").exists(_.text.equals("apple")))
    // 方式二
    val filters = (xml \\ "symbol").filter(x => ((x \ "@ticker").text).equals("apple"))
    for (filter <- filters) {
      println(filter)
    }

    println("----------------------")

    (xml \ "symbol" \ "units").map(x=>(x.text )).foreach(println)
  }

  def loadXML(): Unit = {

    // 方式一
    //    val xml = XML.load(this.getClass.getClassLoader.getResource("test.xml"))
    //    println(xml)

    // 方式二
    //    val xml = XML.load(new FileInputStream(
    //      "/Users/Mac/my-pro-workspace/scalatrain/src/main/resources/test.xml"))
    //    println(xml)

    // 方式三
    val xml = XML.load(new InputStreamReader(new FileInputStream(
      "/Users/Mac/my-pro-workspace/scalatest/src/main/resources/test.xml")))

    println(xml)
  }


}

4)结果

<symbols>
    <symbol ticker="apple">
        <units>200</units>
    </symbol>
    <units>500</units>
    <symbol ticker="thinkpad">
        <units>400</units>
    </symbol>
</symbols>
=======================
<units>200</units><units>400</units>
----------------------
<units>200</units>
<units>500</units>
<units>400</units>
----------------------
apple
thinkpad
----------------------
<symbol ticker="apple">
        <units>200</units>
    </symbol>
----------------------
200
400

Process finished with exit code 0
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值