温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Jython中怎么访问Java属性文件

发布时间:2021-08-11 15:33:37 来源:亿速云 阅读:133 作者:Leah 栏目:编程语言

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

为何需要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属性文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI