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