资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Jython中怎么访问Java属性文件

Jython中怎么访问Java属性文件,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联主营叙永网站建设的网络公司,主营网站建设方案,成都app软件开发,叙永h5微信小程序定制开发搭建,叙永网站营销推广欢迎叙永等地区企业咨询

为何需要Jython访问Java属性文件

使用Jython的过程中经常需要访问Java属性文件以得到配置信息。Jython 可以用loadProperties 和getProperty 函数做到这一点,如下所示:

def loadProperties (source):      """ 将Java属性文件加载入词典 """     result = {}      if type(source) == type(''):    # name provided, use file          source = io.FileInputStream(source)      bis = io.BufferedInputStream(source)      props = util.Properties()      props.load(bis)       bis.close()      for key in props.keySet().iterator():          result[key] = props.get(key)      return result   def getProperty (properties, name, default=None):      """ 取得一个属性 """     return properties.get(name, default)

Jython访问Java属性文件示例

所以,假如使用的是访问Java属性文件 中的函数,如下所示:

import sys     file = sys.argv[1]     props = loadProperties(file)     print "Properties file: %s, contents:" % file      print props     print "Property %s = %i" % ('debug', int(getProperty(props, 'debug', '0')))     import sys  file = sys.argv[1]  props = loadProperties(file)  print "Properties file: %s, contents:" % file   print props  print "Property %s = %i" % ('debug', int(getProperty(props, 'debug', '0')))

并且这些函数具有如下属性文件内容:

# 一个示例用属性文件     debug = 1    error.level = ERROR     now.is.the.time = false     # 一个示例用属性文件  debug = 1 error.level = ERROR  now.is.the.time = false

那么,得到的输出就会是:

Properties file: test.properties, contents:     {'error.level': 'ERROR', 'debug': '1', 'now.is.the.time': 'false'}     Property debug = 1

看完上述内容,你们掌握Jython中怎么访问Java属性文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


当前标题:Jython中怎么访问Java属性文件
链接URL:http://cdkjz.cn/article/iecigi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220