9. Scala操作外部数据 文件读取,xml,Excel,MySQL

戏子无情 提交于 2019-12-05 23:02:14

文件读取

读取本地文件,读取csv文件,读取网络文件

package com.yy.scala

import scala.io.Source

/**
 * 文件读取
 */
object FileApp extends App {

  val file = Source.fromFile("D:\\data\\hello.txt")
  //按行读取
  for(line <- file.getLines()){
    println(line)
  }

  //按字符读取
  for(char <- file){
    println(char)
  }

  //按行读取CSV文件
  val csvFile = Source.fromFile("D:\\data\\test.csv")
  val csvList = csvFile.getLines().toList
  val title = csvList.max
  println("title: "+title)
  val titleArr = title.split(",")
  for(i <- 1 until csvList.size){
    val line = csvList(i)
    val lineList: Array[String] = line.split(",")
    for(i <- 0 until lineList.length){
      print(titleArr(i)+":" + lineList(i) + " ")
    }
    println
  }

  //读取网络文件
  var netFile = Source.fromURL("https://www.baidu.com")
  for(line <- netFile.getLines()){
    println(line)
  }

}

读取xml文件

pom.xml中引入scala-xml工具包

<dependency>
  <groupId>org.scala-lang.modules</groupId>
  <artifactId>scala-xml_2.11</artifactId>
  <version>1.2.0</version>
</dependency>

读取xml文件代码如下

package com.yy.scala

import java.io.{FileInputStream, InputStreamReader}

import scala.xml.XML

object XmlApp extends App {

  //读取XML 项目src/main/resources路径下
  val xml1 = XML.load(this.getClass.getClassLoader.getResource("test.xml"))
  println(xml1)
  //读取XML 绝对路径
  val xml2 = XML.load(new FileInputStream("D:\\data\\test.xml"))
  print(xml2)
  val xml3 = XML.load(new InputStreamReader(new FileInputStream("D:\\data\\test.xml")))
  print(xml3)

  val xml = XML.load(this.getClass.getClassLoader.getResource("test2.xml"))
  //读取header下的filed
  val headerField = xml \ "header" \ "field"
  println(headerField)

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

  //读取header下field的所有name
  //val filedAttributes = (xml \ "header" \ "field").map(_ \ "@name")
  val filedAttributes = (xml \ "header" \ "field" \\ "@name")
  for (filedAttribute <- filedAttributes) {
    println(filedAttribute)
  }

  //读取name=Logon的message
  //val filters = (xml \\ "message").filter(_.attribute("name").exists(_.text.equals("Logon")))
  val filters = (xml \\ "message").filter(x => ((x \ "@name").text).equals("Logon"))
  for (filter <- filters) {
    println(filter)
  }

  //读取header下field的所有name的值,field的值,required的值
  (xml \ "header" \ "field").map(x => (x \ "@name", x.text, x \ "@required")) .foreach(println)

}

读取Excel文件

这里使用的是poi工具类,在pom.xml中引入

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>4.1.0</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>4.1.0</version>
</dependency>

读取Excel文件代码如下

package com.yy.scala

import java.io.FileInputStream

import org.apache.poi.xssf.usermodel.XSSFWorkbook

/**
 * poi读取Excel
 */
object ExcelApp extends App {

  val filePath = "E:\\data\\test.xlsx"
  val fs = new FileInputStream(filePath)
  val xssfWorkbook: XSSFWorkbook = new XSSFWorkbook(fs)
  for(i <- 0 until xssfWorkbook.getNumberOfSheets){
    //获取表格每一个sheet
    val xssfSheet = xssfWorkbook.getSheetAt(i)
    val titleRow = xssfSheet.getRow(0)
    for(row <- 1 to xssfSheet.getLastRowNum()){
      //获取表格每一行
      val xssfRow = xssfSheet.getRow(row)
      for(i <- 0 until xssfRow.getPhysicalNumberOfCells){
        //获取表格每一行的每一列
        val title = titleRow.getCell(i).toString
        val value = xssfRow.getCell(i).toString
        print(title + ":" +value+" " )
      }
      println
    }
  }

}

连接MySQL数据库

在pom.xml中引入MySQL驱动包

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

连接MySQL读取mysql库user表代码如下

package com.yy.scala

import java.sql.{Connection, DriverManager, ResultSet, Statement}

object MySQLApp extends App {
  val url = "jdbc:mysql://127.0.0.1:3306/mysql?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai"
  val username = "root"
  val password = "123456"
  var connection: Connection = null
  var statement:Statement = null
  var resultSet:ResultSet = null
  try {
    classOf[com.mysql.cj.jdbc.Driver]
    //获取连接
    connection = DriverManager.getConnection(url, username, password)
    statement = connection.createStatement()
    resultSet = statement.executeQuery("select host, user from user")
    while (resultSet.next()) {
      val host = resultSet.getString("host")
      val user = resultSet.getString("user")
      println(s"host:$host, user:$user")
    }
  } catch {
    case e: Exception => println(e)
  } finally {
    // 释放资源
    if(resultSet != null){
      resultSet.close()
    }
    if(statement != null){
      statement.close()
    }
    if(connection != null){
      connection.close()
    }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!