2-scala文件操作--自动关闭打开的资源,读取properties文件

简介:

简介

使用scala的loan pattern自动关闭打开的资源

读取properties文件

依赖的jar

使用scala_arm库自动关闭资源文件时,需要引入以下依赖:

<dependency>
			<groupId>com.jsuereth</groupId>
			<artifactId>scala-arm_${scala.binary.version}</artifactId>
			<version>1.4</version>
		</dependency>

示例代码

import java.io.InputStream
import java.util.Properties

import scala.collection.JavaConversions.propertiesAsScalaMap

import resource.managed
import test.Control._

object Control {
  def using[A <: { def close(): Unit }, B](resource: A)(f: A => B): B =
    try {
      f(resource)
    } finally {
      if (resource != null)
        resource.close()
    }
}

object FileOperator extends App {
  val prop = new Properties()
  //读取classpath下的配置文件
  //读取properties文件
  //使用scala_arm自动关闭打开的文件资源
  for (
    source <- managed(FileOperator.getClass.getClassLoader.getResourceAsStream("db.properties"))
  ) {
    printProp(source)
  }

  //使用using自动关闭打开的文件资源
  using(FileOperator.getClass.getClassLoader.getResourceAsStream("db.properties")) { source =>
    {
      printProp(source)
    }
  }
  //使用绝对路径读取文件
  val res = readTextFile("D:/scalawork/scala-learn/src/main/resources/db.properties")
  res match {
    case Some(lines) => lines.foreach(println)
    case None        => println("couldn't read file")
  }

  //处理异常,返回Option
  def readTextFile(filename: String): Option[List[String]] = {
    try {
      val lines = using(io.Source.fromFile(filename)) { source =>
        (for (line <- source.getLines) yield line).toList
      }
      Some(lines)
    } catch {
      case e: Exception => None
    }
  }

  //加载properties并转换为HashMap,读取其内容
  def printProp(source: InputStream) = {
    prop.load(source)
    println(prop.getProperty("jdbc.idleMaxAge"))
    prop.foreach(ele => {
      println(s"${ele._1} => ${ele._2}")
    })
  }
}

目录
相关文章
|
存储 SQL 分布式计算
Flink - 读取 Parquet 文件 By Scala / Java
parquet 文件常见与 Flink、Spark、Hive、Streamin、MapReduce 等大数据场景,通过列式存储和元数据存储的方式实现了高效的数据存储与检索,下面介绍 Flink 场景下如何读取 Parquet。
1519 0
Flink - 读取 Parquet 文件 By Scala / Java
|
4月前
|
Scala
scala 读取文件(中文)异常 thread "main" java.nio.charset.MalformedInputException: Input length = 1
scala 读取文件(中文)异常 thread "main" java.nio.charset.MalformedInputException: Input length = 1
41 0
|
分布式计算 Java Scala
一天学完spark的Scala基础语法教程十三、文件IO操作(idea版本)
一天学完spark的Scala基础语法教程十三、文件IO操作(idea版本)
111 0
一天学完spark的Scala基础语法教程十三、文件IO操作(idea版本)
|
Java 编译器 Scala
【Scala】Scala之Classes and Properties(一)
前面学习了控制结构,下面学习Scala的Class和Properties。
160 0
【Scala】Scala之Classes and Properties(一)
|
算法 Java 编译器
【Scala】Scala之Classes and Properties(二)
前面学习了控制结构,下面学习Scala的Class和Properties。
96 0
【Scala】Scala之Classes and Properties(二)
|
Scala
scala小工具:自动生成跑批文件
由于数仓项目中经常需要手工跑批补历史数据,通过excel手工拉取每次都要写函数不太方便,通过小工具生成,省时省力。
254 0
|
大数据 Scala 分布式计算
大数据Scala系列之文件以及正则表达式
大数据Scala系列之文件以及正则表达式1 读取行导入scala.io.Source后,即可引用Source中的方法读取文件信息。 import scala.io.Sourceobject FileDemo extends App{ val source = Source.
1382 0
|
Scala Java
[Scala]Scala学习笔记六 文件
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/76574187 1.
872 0