我刚接触 Scala 编程,想要在 Scala 中读取属性文件。
我找不到任何用于在 Scala 中读取属性文件的 API。
请告诉我是否存在此类 API 或其他读取属性文件的方式。
我刚接触 Scala 编程,想要在 Scala 中读取属性文件。
我找不到任何用于在 Scala 中读取属性文件的 API。
请告诉我是否存在此类 API 或其他读取属性文件的方式。
你需要以类似于将 Scala Map
转换为 java.util.Map
的方式来处理它。 java.util.Properties
扩展了 java.util.HashTable
,而 java.util.HashTable
又扩展了 java.util.Dictionary
。
scala.collection.JavaConverters
具有将 Dictionary
转换为 Scala mutable.Map
并相反的功能:
val x = new Properties
//load from .properties file here.
import scala.collection.JavaConverters._
scala> x.asScala
res4: scala.collection.mutable.Map[String,String] = Map()
您可以使用上述的Map
来获取和检索。但如果您希望将其转换回Properties
类型(以进行存储等操作),则可能需要手动进行类型转换。
考虑一些类似的事情
def getPropertyX: Option[String] = Source.fromFile(fileName)
.getLines()
.find(_.startsWith("propertyX="))
.map(_.replace("propertyX=", ""))
properties.load(this.getClass.getResourceAsStream("/path/to/file.properties"))```
- Pardeep