临时接到任务,要求工程的配置文件由.conf改为.properties.
具体原因是获取conf的api在值为xx,xx时必须加引号,否则出错,与南京项目组冲突,鉴于api已经固化在jar里,所以要修改.查了下java里的api,小小修改为scala代码
import java.util.Properties
import java.io.FileInputStream
//test.properties 里的内容为"ddd=5.6,1.2"
def loadProperties():Unit = {
val properties = new Properties()
val path = Thread.currentThread().getContextClassLoader.getResource("test.properties").getPath //文件要放到resource文件夹下
properties.load(new FileInputStream(path))
println(properties.getProperty("ddd"))//读取键为ddd的数据的值
println(properties.getProperty("ddd","没有值"))//如果ddd不存在,则返回第二个参数
properties.setProperty("ddd","123")//添加或修改属性值
}
实现很简单,更多操作以后再查询.