Reputation: 1279
I am new to Scala programming and I wanted to read a properties file in Scala.
I can't find any APIs to read a property file in Scala.
Please let me know if there are any API for this or other way to read properties files in Scala.
Upvotes: 25
Views: 36208
Reputation: 1969
Consider something along the lines
def getPropertyX: Option[String] = Source.fromFile(fileName)
.getLines()
.find(_.startsWith("propertyX="))
.map(_.replace("propertyX=", ""))
Upvotes: 2
Reputation: 21567
Beside form Java API, there is a library by Typesafe called config with a good API for working with configuration files of different types.
Upvotes: 23
Reputation: 31744
You will have to do it in similar way you would with with Scala Map
to java.util.Map
. java.util.Properties
extends java.util.HashTable
whiche extends java.util.Dictionary
.
scala.collection.JavaConverters
has functions to convert to and fro from Dictionary
to 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()
You can then use Map
above. To get and retrieve. But if you wish to convert it back to Properties
type (to store back etc), you might have to type cast it manually then.
Upvotes: 18