国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java 操作Properties配置文件詳解

瀏覽:72日期:2022-08-27 16:56:58

java中的properties文件是一種配置文件,主要用于表達(dá)配置信息,文件類型為*.properties,格式為文本文件,文件的內(nèi)容是格式是'鍵=值'的格式,在properties

文件中,可以用'#'來(lái)作注釋,properties文件在Java編程中用到的地方很多,操作很方便。

一、properties文件

test.properties------------------------------------------------------################################## 工商報(bào)表應(yīng)用IcisReport的配置文件## 日期:2006年11月21日 #################################### 說(shuō)明:業(yè)務(wù)系統(tǒng)TopIcis和報(bào)表系統(tǒng)IcisReport是分離的# 可分開(kāi)部署到不同的服務(wù)器上,也可以部署到同一個(gè)服務(wù)# 器上;IcisReprot作為獨(dú)立的web應(yīng)用程序可以使用任何# 的Servlet容器或者J2EE服務(wù)器部署并單獨(dú)運(yùn)行,也可以# 通過(guò)業(yè)務(wù)系統(tǒng)的接口調(diào)用作為業(yè)務(wù)系統(tǒng)的一個(gè)庫(kù)來(lái)應(yīng)用.## IcisReport的ipIcisReport.server.ip=192.168.3.143# IcisReport的端口IcisReport.server.port=8080# IcisReport的上下文路徑IcisReport.contextPath=/IcisReport------------------------------------------------------

Properties類的重要方法

Properties類存在于胞Java.util 中,該類繼承自 Hashtable1. getProperty ( String key) , 用指定的鍵在此屬性列表中搜索屬性。也就是通過(guò)參數(shù) key ,得到 key 所對(duì)應(yīng)的 value。2. load ( InputStream inStream) ,從輸入流中讀取屬性列表(鍵和元素對(duì))。通過(guò)對(duì)指定的文件(比如說(shuō)上面的 test.properties文件)進(jìn)行裝載來(lái)獲取該文件中的所有鍵 - 值對(duì)。以供 getProperty ( String key) 來(lái)搜索。3. setProperty ( String key, String value) ,調(diào)用 Hashtable 的方法 put 。他通過(guò)調(diào)用基類的put方法來(lái)設(shè)置 鍵 - 值對(duì)。4. store ( OutputStream out, String comments) , 以適合使用 load 方法加載到Properties表中的格式,將此Properties表中的屬性列表(鍵和元素對(duì))寫入輸出流。與 load 方法相反,該方法將鍵 - 值對(duì)寫入到指定的文件中去。5. clear () ,清除所有裝載的 鍵 - 值對(duì)。該方法在基類中提供。-------------------------------

二、操作properties文件的java方法

讀屬性文件Properties prop = new Properties();InputStream in = getClass().getResourceAsStream('/IcisReport.properties');prop.load(in);Set keyValue = prop.keySet();for (Iterator it = keyValue.iterator(); it.hasNext();){String key = (String) it.next();}------------------------outputFile = new FileOutputStream(fileName);propertie.store(outputFile, description);outputFile.close();-----------------------------------------------------------------------------------------Class.getResourceAsStream ('/some/pkg/resource.properties');ClassLoader.getResourceAsStream ('some/pkg/resource.properties');java.util.ResourceBundle rs = java.util.ResourceBundle.getBundle('some.pkg.resource');rs.getString('xiaofei');-----------------------------------------------------------------------------------------寫屬性文件Configuration saveCf = new Configuration();saveCf.setValue('min', '10');saveCf.setValue('max', '1000');saveCf.saveFile('.configsave.perperties','test');

總結(jié):java的properties文件需要放到classpath下面,這樣程序才能讀取到,有關(guān)classpath實(shí)際上就是java類或者庫(kù)的存放路徑,在java工程中,properties放到class文件一塊。在web應(yīng)用中,最簡(jiǎn)單的方法是放到web應(yīng)用的WEB- INFclasses目錄下即可,也可以放在其他文件夾下面,這時(shí)候需要在設(shè)置classpath環(huán)境變量的時(shí)候,將這個(gè)文件夾路徑加到 classpath變量中,這樣也也可以讀取到。在此,你需要對(duì)classpath有個(gè)深刻理解,classpath絕非系統(tǒng)中刻意設(shè)定的那個(gè)系統(tǒng)環(huán)境變量,WEB-INFclasses其實(shí)也是,java工程的class文件目錄也是。

發(fā)個(gè)例子大家自己看哈.

package control;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.Properties;public class TestMain { //根據(jù)key讀取value public static String readValue(String filePath,String key) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream (new FileInputStream(filePath)); props.load(in); String value = props.getProperty (key); System.out.println(key+value); return value; } catch (Exception e) { e.printStackTrace(); return null; } } //讀取properties的全部信息 public static void readProperties(String filePath) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream (new FileInputStream(filePath)); props.load(in); Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty (key); System.out.println(key+Property);} } catch (Exception e) { e.printStackTrace(); } } //寫入properties信息 public static void writeProperties(String filePath,String parameterName,String parameterValue) { Properties prop = new Properties(); try { InputStream fis = new FileInputStream(filePath); //從輸入流中讀取屬性列表(鍵和元素對(duì)) prop.load(fis); //調(diào)用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 //強(qiáng)制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調(diào)用 put 的結(jié)果。 OutputStream fos = new FileOutputStream(filePath); prop.setProperty(parameterName, parameterValue); //以適合使用 load 方法加載到 Properties 表中的格式, //將此 Properties 表中的屬性列表(鍵和元素對(duì))寫入輸出流 prop.store(fos, 'Update ’' + parameterName + '’ value'); } catch (IOException e) { System.err.println('Visit '+filePath+' for updating '+parameterName+' value error'); } } public static void main(String[] args) { readValue('info.properties','url'); writeProperties('info.properties','age','21'); readProperties('info.properties' ); System.out.println('OK'); }

發(fā)個(gè)例子大家自己看哈.

package control;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.Properties;public class TestMain { //根據(jù)key讀取value public static String readValue(String filePath,String key) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream (new FileInputStream(filePath)); props.load(in); String value = props.getProperty (key); System.out.println(key+value); return value; } catch (Exception e) { e.printStackTrace(); return null; } } //讀取properties的全部信息 public static void readProperties(String filePath) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream (new FileInputStream(filePath)); props.load(in); Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty (key); System.out.println(key+Property);} } catch (Exception e) { e.printStackTrace(); } } //寫入properties信息 public static void writeProperties(String filePath,String parameterName,String parameterValue) { Properties prop = new Properties(); try { InputStream fis = new FileInputStream(filePath); //從輸入流中讀取屬性列表(鍵和元素對(duì)) prop.load(fis); //調(diào)用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 //強(qiáng)制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調(diào)用 put 的結(jié)果。 OutputStream fos = new FileOutputStream(filePath); prop.setProperty(parameterName, parameterValue); //以適合使用 load 方法加載到 Properties 表中的格式, //將此 Properties 表中的屬性列表(鍵和元素對(duì))寫入輸出流 prop.store(fos, 'Update ’' + parameterName + '’ value'); } catch (IOException e) { System.err.println('Visit '+filePath+' for updating '+parameterName+' value error'); } } public static void main(String[] args) { readValue('info.properties','url'); writeProperties('info.properties','age','21'); readProperties('info.properties' ); System.out.println('OK'); }}

到此這篇關(guān)于Java 操作Properties配置文件詳解的文章就介紹到這了,更多相關(guān)Java 操作Properties配置文件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 延寿县| 铜鼓县| 绥中县| 台江县| 都江堰市| 怀化市| 松潘县| 南川市| 建湖县| 微博| 郓城县| 大竹县| 石泉县| 黄大仙区| 乐平市| 资溪县| 遂平县| 巧家县| 葫芦岛市| 淮滨县| 上栗县| 遂川县| 延津县| 巴中市| 安多县| 甘泉县| 徐汇区| 将乐县| 瑞昌市| 万山特区| 颍上县| 永福县| 馆陶县| 昌平区| 岳池县| 施秉县| 岳普湖县| 南汇区| 桦川县| 新蔡县| 竹溪县|