国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

經(jīng)營(yíng)性網(wǎng)站可以進(jìn)行非經(jīng)營(yíng)行網(wǎng)站備案嗎代刷網(wǎng)站推廣快速

經(jīng)營(yíng)性網(wǎng)站可以進(jìn)行非經(jīng)營(yíng)行網(wǎng)站備案嗎,代刷網(wǎng)站推廣快速,秀米編輯器官網(wǎng),網(wǎng)站建設(shè)單一來(lái)源談判文件文章目錄 概述常見(jiàn)方法寫(xiě)入讀取遍歷 概述 Properties 繼承于 Hashtable。表示一個(gè)持久的屬性集,屬性列表以key-value的形式存在,key和value都是字符串。 Properties 類被許多Java類使用。例如,在獲取環(huán)境變量時(shí)它就作為System.getPropertie…

文章目錄

  • 概述
  • 常見(jiàn)方法
  • 寫(xiě)入
  • 讀取
  • 遍歷

概述

Properties 繼承于 Hashtable。表示一個(gè)持久的屬性集,屬性列表以key-value的形式存在,key和value都是字符串。

Properties 類被許多Java類使用。例如,在獲取環(huán)境變量時(shí)它就作為System.getProperties()方法的返回值。

我們?cè)诤芏嘈枰苊庥簿幋a的應(yīng)用場(chǎng)景下需要使用properties文件來(lái)加載程序需要的配置信息,比如 JDBC、MyBatis框架等。Properties類則是properties文件和程序的中間橋梁,不論是從properties文件讀取信息還是寫(xiě)入信息到properties文件都要經(jīng)由Properties類。

常見(jiàn)方法

除了從Hashtable中所定義的方法,Properties定義了以下方法:

String getProperty(String key)用指定的鍵在此屬性列表中搜索屬性。
String getProperty(String key,String defaultPproperty)用指定的鍵在屬性列表中搜索屬性。
void list(PrintStream streamOut)將屬性列表輸出到指定的輸出流。
void list(PrintWriter streamOut)將屬性列表輸出到指定的輸出流。
voi load(InputStream streamIn) throws IOException從輸入流中讀取屬性列表(鍵和元素對(duì))。
Enumeration propertyNames()按簡(jiǎn)單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素)
Object setProperty(String key, String value)調(diào)用Hashtable的方法put
void store(OutputStream streamOut, String description)以適合使用load(InputStream)方法加載到Properties表中的格式,將此Properties表中的屬性列表(鍵和元素)寫(xiě)入輸出流。

Properties類

下面我們從寫(xiě)入、讀取、遍歷等角度來(lái)解析Properties類的常見(jiàn)用法:

寫(xiě)入

Properties類調(diào)用setProperty方法將鍵值對(duì)保存到內(nèi)存中,此時(shí)可以通過(guò)getProperty方法讀取,propertyNames方法進(jìn)行遍歷,但是并沒(méi)有將鍵值對(duì)持久化到屬性文件中,故需要調(diào)用store方法持久化鍵值對(duì)到屬性文件中。

package cn.htl;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import junit.framework.TestCase;public class PropertiesTester extends TestCase {public void writeProperties() {Properties properties = new Properties();OutputStream output = null;try {output = new FileOutputStream("config.properties");properties.setProperty("url", "jdbc:mysql://localhost:3306/");properties.setProperty("username", "root");properties.setProperty("password", "root");properties.setProperty("database", "users");//保存鍵值對(duì)到內(nèi)存properties.store(output, "Steven1997 modify" + newDate().toString());// 保存鍵值對(duì)到文件中} catch (IOException io) {io.printStackTrace();} finally {if (output != null) {try {output.close();} catch (IOException e) {e.printStackTrace();}}}}
}

讀取

下面給出常見(jiàn)的六種讀取properties文件的方式:

package cn.htl;import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;/**
* 讀取properties文件的方式
*
*/
public class LoadPropertiesFileUtil {private static String basePath = "src/main/java/cn/habitdiary/prop.properties";private static String path = "";/*** 一、 使用java.util.Properties類的load(InputStream in)方法加載properties文件** @return*/public static String getPath1() {try {InputStream in = new BufferedInputStream(new FileInputStream(new File(basePath)));Properties prop = new Properties();prop.load(in);path = prop.getProperty("path");} catch (FileNotFoundException e) {System.out.println("properties文件路徑書(shū)寫(xiě)有誤,請(qǐng)檢查!");e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return path;}/*** 二、 使用java.util.ResourceBundle類的getBundle()方法* 注意:這個(gè)getBundle()方法的參數(shù)只能寫(xiě)成包路徑+properties文件名,否則將拋異常** @return*/public static String getPath2() {ResourceBundle rb = ResourceBundle.getBundle("cn/habitdiary/prop");path = rb.getString("path");return path;}/*** 三、 使用java.util.PropertyResourceBundle類的構(gòu)造函數(shù)** @return*/public static String getPath3() {InputStream in;try {in = new BufferedInputStream(new FileInputStream(basePath));ResourceBundle rb = new PropertyResourceBundle(in);path = rb.getString("path");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {e.printStackTrace();}return path;}/*** 四、 使用class變量的getResourceAsStream()方法* 注意:getResourceAsStream()方法的參數(shù)按格式寫(xiě)到包路徑+properties文件名+.后綴** @return*/public static String getPath4() {InputStream in = LoadPropertiesFileUtil.class.getResourceAsStream("cn/habitdiary/prop.properties");Properties p = new Properties();try {p.load(in);path = p.getProperty("path");} catch (IOException e) {e.printStackTrace();}return path;   }/*** 五、* 使用class.getClassLoader()所得到的java.lang.ClassLoader的* getResourceAsStream()方法* getResourceAsStream(name)方法的參數(shù)必須是包路徑+文件名+.后綴* 否則會(huì)報(bào)空指針異常* @return*/public static String getPath5() {InputStream in = LoadPropertiesFileUtil.class.getClassLoader().getResourceAsStream("cn/habitdiary/prop.properties");Properties p = new Properties();try {p.load(in);path = p.getProperty("path");} catch (IOException e) {e.printStackTrace();}return path;}/*** 六、 使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態(tài)方法* getSystemResourceAsStream()方法的參數(shù)格式也是有固定要求的** @return*/public static String getPath6() {InputStream in = ClassLoader.getSystemResourceAsStream("cn/habitdiary/prop.properties");Properties p = new Properties();try {p.load(in);path = p.getProperty("path");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return path;}public static void main(String[] args) {System.out.println(LoadPropertiesFileUtil.getPath1());System.out.println(LoadPropertiesFileUtil.getPath2());System.out.println(LoadPropertiesFileUtil.getPath3());System.out.println(LoadPropertiesFileUtil.getPath4());System.out.println(LoadPropertiesFileUtil.getPath5());System.out.println(LoadPropertiesFileUtil.getPath6());}}

其中第一、四、五、六種方式都是先獲得文件的輸入流,然后通過(guò)Properties類的load(InputStreaminStream)方法加載到Properties對(duì)象中,最后通過(guò)Properties對(duì)象來(lái)操作文件內(nèi)容。

第二、三中方式是通過(guò)ResourceBundle類來(lái)加載Properties文件,然后ResourceBundle對(duì)象來(lái)操做properties文件內(nèi)容。

其中最重要的就是每種方式加載文件時(shí),文件的路徑需要按照方法的定義的格式來(lái)加載,否則會(huì)拋出各種異常,比如空指針異常。

遍歷

下面給出四種遍歷Properties中的所有鍵值對(duì)的方法:

/**
* 輸出properties的key和value
*/
public static void printProp(Properties properties) {System.out.println("---------(方式一)------------");for (String key : properties.stringPropertyNames()) {System.out.println(key + "=" + properties.getProperty(key));}System.out.println("---------(方式二)------------");Set<Object> keys = properties.keySet();//返回屬性key的集合for (Object key : keys) {System.out.println(key.toString() + "=" + properties.get(key));}System.out.println("---------(方式三)------------");Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();//返回的屬性鍵值對(duì)實(shí)體for (Map.Entry<Object, Object> entry : entrySet) {System.out.println(entry.getKey() + "=" + entry.getValue());}System.out.println("---------(方式四)------------");Enumeration<?> e = properties.propertyNames();while (e.hasMoreElements()) {String key = (String) e.nextElement();String value = properties.getProperty(key);System.out.println(key + "=" + value);}
}
http://m.aloenet.com.cn/news/37382.html

相關(guān)文章:

  • 站點(diǎn)推廣是什么意思關(guān)鍵詞密度
  • 怎么搭建網(wǎng)站后臺(tái)日本比分算1:1
  • 做印刷網(wǎng)站公司哪家好熱詞搜索排行榜
  • 做動(dòng)漫的網(wǎng)站長(zhǎng)沙網(wǎng)絡(luò)營(yíng)銷公司
  • 南京建設(shè)工程交易中心網(wǎng)站seo的內(nèi)容主要有哪些方面
  • 做茶葉網(wǎng)站的素材上海網(wǎng)絡(luò)公司seo
  • 做公司網(wǎng)站需要多少錢杭州百度首頁(yè)優(yōu)化
  • 廊坊網(wǎng)站關(guān)鍵詞優(yōu)化seo關(guān)鍵詞是什么
  • 如何在騰訊云做網(wǎng)站福州排名seo公司
  • 制作網(wǎng)站的知識(shí)免費(fèi)推廣方式有哪些
  • 做網(wǎng)站前端網(wǎng)絡(luò)營(yíng)銷試題庫(kù)及答案
  • 做噯噯的網(wǎng)站科學(xué)新概念外鏈平臺(tái)
  • 第三方網(wǎng)站開(kāi)發(fā)優(yōu)缺點(diǎn)銷售渠道及方式
  • 廣告圖文制作用哪個(gè)軟件seoul是啥意思
  • 網(wǎng)站開(kāi)發(fā)和app的區(qū)別優(yōu)化大師使用方法
  • 不用編程做APP和響應(yīng)式網(wǎng)站百度手機(jī)助手下載安卓版
  • 買公司的網(wǎng)站建設(shè)軟文寫(xiě)作營(yíng)銷
  • 銅川網(wǎng)站建設(shè)報(bào)價(jià)網(wǎng)絡(luò)推廣的主要工作內(nèi)容
  • 專門做網(wǎng)站需要敲代碼么旺道seo優(yōu)化軟件怎么用
  • 做網(wǎng)站宣傳圖片google全球推廣
  • 珠海建網(wǎng)站專業(yè)公司專業(yè)seo優(yōu)化推廣
  • 如何做網(wǎng)站條幅閃圖搜索關(guān)鍵詞怎么讓排名靠前
  • wordpress頭像網(wǎng)站百度ai人工智能平臺(tái)
  • 該怎么給做網(wǎng)站的提頁(yè)面需求深圳網(wǎng)絡(luò)推廣網(wǎng)絡(luò)
  • 廣東省廣州市白云區(qū)太和鎮(zhèn)名風(fēng)seo軟件
  • 中新生態(tài)城建設(shè)局門戶網(wǎng)站昆明seo案例
  • 佛山網(wǎng)站建設(shè)在哪手機(jī)優(yōu)化
  • 重慶網(wǎng)站建設(shè)公司銷售seo營(yíng)銷推廣多少錢
  • 珠海企業(yè)網(wǎng)站建設(shè)seo優(yōu)化seo外包
  • b2b網(wǎng)站怎么發(fā)布信息站長(zhǎng)之家論壇