【Scala】读写Propertie后缀的配置文件

PropertiesUtil.scala

import java.io.InputStreamReader
import java.util.Properties

/**
 * @author Uni
 * @create 2021/12/2 12:37
 * 读取 properties 配置文件的工具类
 */
object PropertiesUtil {
  def main(args: Array[String]): Unit = {
    val properties : Properties = PropertiesUtil.load("config.properties")
    println(properties.getProperty("kafka.broker.list"))
  }
  def load(propertieName: String): Properties = {
    val prop = new Properties()
    prop.load(new InputStreamReader(Thread.currentThread().
      getContextClassLoader.
      getResourceAsStream(propertieName),
      "UTF-8"))
    prop
  }
}

测试文本

config.properties

# Kafka 配置
kafka.broker.list=hadoop101:9092,hadoop102:9092,hadoop103:9092

输出结果

hadoop101:9092,hadoop102:9092,hadoop103:9092
上一篇:centos6、7、redhat linux系统5步骤自动备份mysql数据库脚本,简单易用


下一篇:谷歌地图分辨率表