Skip to content

Instantly share code, notes, and snippets.

@ninthdrug
Last active October 18, 2019 06:35
Show Gist options
  • Save ninthdrug/4068637 to your computer and use it in GitHub Desktop.
Save ninthdrug/4068637 to your computer and use it in GitHub Desktop.
Parsing java properties files with Scala
import scala.io.Source.fromFile
def parseProperties(filename: String): Map[String,String] = {
val lines = fromFile(filename).getLines.toSeq
val cleanLines = lines.map(_.trim).filter(!_.startsWith("#")).filter(_.contains("="))
cleanLines.map(line => { val Array(a,b) = line.split("=",2); (a.trim, b.trim)}).toMap
}
@vijaybritto
Copy link

vijaybritto commented Oct 18, 2019

you gotta close the file to prevent a leak.
can be split like

                val file = fromFile(filename)
		val lines = file.getLines.toSeq
		file.close()
		val cleanLines = lines.map(_.trim).filter(!_.startsWith("#")).filter(_.contains("="))
		cleanLines.map(
			line => {
				val Array(a,b) = line.split("=",2)
				(a.trim, b.trim)
			}
		).toMap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment