方法不同, 能读取的文件路径和文件类型也不同。
Java 读取 .properties 配置文件的几种方式 - 暴脾气大大 - 博客园
-
1.1 scala语言读取1) 读取任意路径下的properties文件
import java.io.{BufferedReader, FileReader}
import java.util.Properties
object ConfManager {
private val prop: Properties = new Properties
def readConf(file_path: String): Unit = {
try {
val in = new BufferedReader(new FileReader(file_path))
prop.load(in)
in.close()
} catch {
case e: Exception => {
System.err.println("读取配置文件失败!")
System.exit(1)
}
}
}
def getString(key: String): String = {
val value = prop.getProperty(key)
if (null == value) {
System.err.println(s"${key} 没有赋值")
System.exit(1)
}
value
}
def getInt(key: String): Int = {
getString(key).toInt
}
}
2) 只能读取resource目录下的
import java.io.{BufferedReader, FileReader}
import java.util.Properties
object ConfManager {
private val prop: Properties = new Properties
def readConf(file_path: String): Unit = {
try {
val in = Thread.currentThread().getContextClassLoader.getResourceAsStream( "config.properties" )
prop.load( in )
in.close()
} catch {
case e: Exception => {
System.err.println("读取配置文件失败!")
System.exit(1)
}
}
}
def getString(key: String): String = {
val value = prop.getProperty(key)
if (null == value) {
System.err.println(s"${key} 没有赋值")
System.exit(1)
}
value
}
def getInt(key: String): Int = {
getString(key).toInt
}
}
2 获取环境变量
# shell
if [[ ${AWS_DEFAULT_REGION} = eu-west-1 ]]
then
region_flag="ireland"
elif [[ ${AWS_DEFAULT_REGION} = ap-south-1 ]]
then
region_flag="india"
else
echo "error:获取了错误的区域标记"
exit 1
fi
# scala
val region = sys.env("AWS_DEFAULT_REGION")



