自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Jython訪問Java屬性文件的方法一覽

開發(fā) 后端
本文介紹了Jython訪問Java屬性文件的方法。在Jython中可以通過loadProperties 和getProperty 函數(shù)做到這一點(diǎn)。

為何需要Jython訪問Java屬性文件

使用Jython的過程中經(jīng)常需要訪問Java屬性文件以得到配置信息。Jython 可以用loadProperties 和getProperty 函數(shù)做到這一點(diǎn),如下所示:

  1. def loadProperties (source):  
  2.     """ 將Java屬性文件加載入詞典 """ 
  3.     result = {}  
  4.     if type(source) == type(''):    # name provided, use file  
  5.         source = io.FileInputStream(source)  
  6.     bis = io.BufferedInputStream(source)  
  7.     props = util.Properties()  
  8.     props.load(bis)   
  9.     bis.close()  
  10.     for key in props.keySet().iterator():  
  11.         result[key] = props.get(key)  
  12.     return result  
  13.  
  14. def getProperty (properties, name, default=None):  
  15.     """ 取得一個屬性 """ 
  16.     return properties.get(name, default)  

Jython訪問Java屬性文件示例

所以,假如使用的是訪問Java屬性文件 中的函數(shù),如下所示:

  1. import sys     
  2. file = sys.argv[1]     
  3. props = loadProperties(file)     
  4. print "Properties file: %s, contents:" % file      
  5. print props     
  6. print "Property %s = %i" % ('debug', int(getProperty(props, 'debug''0')))    
  7.  
  8. import sys  
  9. file = sys.argv[1]  
  10. props = loadProperties(file)  
  11. print "Properties file: %s, contents:" % file   
  12. print props  
  13. print "Property %s = %i" % ('debug', int(getProperty(props, 'debug''0')))  

并且這些函數(shù)具有如下屬性文件內(nèi)容:

  1. # 一個示例用屬性文件     
  2. debug = 1    
  3. error.level = ERROR     
  4. now.is.the.time = false    
  5.  
  6. # 一個示例用屬性文件  
  7. debug = 1 
  8. error.level = ERROR  
  9. now.is.the.time = false  

那么,得到的輸出就會是:

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

以上就是Jython訪問Java屬性文件的實(shí)現(xiàn)方法。

【編輯推薦】

  1. 用于Jython連接Java的JavaUtils模塊
  2. Jython線程示例:定義共享緩沖區(qū)
  3. 與Java語言相比Jython開發(fā)的獨(dú)特性能
  4. 與Java相比Jython性能表現(xiàn)
  5. 在代碼中深入學(xué)習(xí)Jython語法
責(zé)任編輯:yangsai 來源: 網(wǎng)絡(luò)
相關(guān)推薦

2010-09-14 13:13:12

sql server備

2021-06-08 09:47:44

Java面向對象

2009-07-09 17:40:26

Java運(yùn)算符

2009-08-17 17:19:00

ASP.NET緩存數(shù)據(jù)

2009-03-03 20:44:06

桌面虛擬化Xendesktop虛擬化

2010-10-14 16:55:00

MySQL聯(lián)結(jié)查詢

2017-03-06 16:34:12

虛擬個人助理

2020-02-17 15:29:00

石墨文檔

2010-11-15 09:55:35

Oracle轉(zhuǎn)換函數(shù)

2023-11-08 07:45:47

Spring微服務(wù)

2011-01-11 09:53:28

linux進(jìn)程

2011-01-11 10:06:14

linux進(jìn)程

2019-04-26 14:21:34

手機(jī)色彩蘋果

2009-06-22 17:20:33

J2EE框架

2014-08-11 11:19:53

Java虛擬機(jī)Java

2010-10-21 15:40:05

SQL Server服

2017-09-06 08:12:43

OpenStack功能模塊

2011-05-26 10:27:37

Fedora 15

2017-04-25 17:29:24

2012-02-20 09:06:20

JVM
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號