scala读取配置文件
/*利用Typesafe的Config库*/
/*不够灵活,读取的文件内容单一,容易报错*/
/*import com.typesafe.config.{Config,ConfigFactory}
object ConfigUtil2{
private val config: Config = ConfigFactory.load("vbs.conf")
val redis_server_ip=config.getString("redis.server.ip")
def main(args: Array[String]): Unit = {
println(redis_server_ip)
}
}*/
/*使用类加载器读取配置文件*/
object ConfigUtil1{
val is = ConfigUtil1.getClass.getClassLoader.getResourceAsStream("vbs.conf")
private val props = new Properties()
props.load(is)
def getProperty(key:String): String = props.getProperty(key)
def main(args: Array[String]): Unit = {
println(ConfigUtil1.getProperty("redis.server.ip"))
println(ConfigUtil1.getProperty("redis.server.port"))
println(ConfigUtil1.getProperty("test_path"))
}
}
本文来自博客园,作者:diablo-427,转载请注明原文链接:https://www.cnblogs.com/diablo-427/articles/16770789.html