scala 如何在scala中读取属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17873162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:32:59  来源:igfitidea点击:

how to read properties file in scala

scala

提问by sunil

I am new to Scala programming and I wanted to read a properties file in Scala.

我是 Scala 编程的新手,我想在 Scala 中读取属性文件。

I can't find any APIs to read a property file in Scala.

我找不到任何 API 来读取 Scala 中的属性文件。

Please let me know if there are any API for this or other way to read properties files in Scala.

请让我知道是否有任何 API 可以通过这种方式或其他方式读取 Scala 中的属性文件。

回答by 4lex1v

Beside form Java API, there is a library by Typesafe called configwith a good API for working with configuration files of different types.

除了 Java API,Typesafe 还有一个名为config的库,它有一个很好的 API 来处理不同类型的配置文件。

回答by Jatin

You will have to do it in similar way you would with with Scala Mapto java.util.Map. java.util.Propertiesextends java.util.HashTablewhiche extends java.util.Dictionary.

您必须以与使用 Scala Mapto java.util.Map. java.util.Properties延伸java.util.HashTablewhiche 延伸java.util.Dictionary

scala.collection.JavaConvertershas functions to convert to and fro from Dictionaryto Scala mutable.Map:

scala.collection.JavaConverters具有Dictionary在 Scala和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 Mapabove. To get and retrieve. But if you wish to convert it back to Propertiestype (to store back etc), you might have to type cast it manually then.

然后就可以使用Map上面的了。获取和检索。但是,如果您希望将其转换回Properties类型(以存储回等),那么您可能必须手动进行类型转换。

回答by Jens Schauder

You can just use the Java API.

您可以只使用Java API

回答by Raul

Consider something along the lines

考虑一些事情

def getPropertyX: Option[String] = Source.fromFile(fileName)
  .getLines()
  .find(_.startsWith("propertyX="))
  .map(_.replace("propertyX=", ""))