加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala:加载Java属性

发布时间:2020-12-16 18:27:28 所属栏目:安全 来源:网络整理
导读:什么是易于阅读和理解,Scala代码根据以下 Java代码加载 Java属性: try { Properties prop = new Properties(); prop.load(new FileInputStream("config.properties")); this.host = prop.getProperty("mongo.host"); this.port = new Integer(prop.getPrope
什么是易于阅读和理解,Scala代码根据以下 Java代码加载 Java属性:

try {
        Properties prop = new Properties();
        prop.load(new FileInputStream("config.properties"));
        this.host = prop.getProperty("mongo.host");
        this.port = new Integer(prop.getProperty("mongo.port"));
        this.dbName = prop.getProperty("mongo.db");
        this.docsCollName  = prop.getProperty("mongo.coll.docs");
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

谢谢!

解决方法

大致相同(如果你没有使用任何配置库):

val (host,port,dbName,docsCollName) = 
  try {
    val prop = new Properties()
    prop.load(new FileInputStream("config.properties"))

    (
      prop.getProperty("mongo.host"),new Integer(prop.getProperty("mongo.port")),prop.getProperty("mongo.db"),prop.getProperty("mongo.coll.docs")
    ) 
    } catch { case e: Exception => 
      e.printStackTrace()
      sys.exit(1)
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读