bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

如何在Java中使用Property類

本篇文章為大家展示了如何在Java中使用Property類,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

創新互聯建站主營古城網站建設的網絡公司,主營網站建設方案,成都App定制開發,古城h5重慶小程序開發公司搭建,古城網站營銷推廣歡迎古城等地區企業咨詢

概念理解

Properties 繼承于 Hashtable。表示一個持久的屬性集,屬性列表以key-value的形式存在,key和value都是字符串。Properties類被許多Java類使用。例如,在獲取環境遍歷時它就作為System.getProperties()方法的返回值。我們在很多需要避免硬編碼的應用場景下需要使用Properties文件來加載程序需要配置的信息,比如JDBC、MyBatis框架等。Properties類則是Properties文件和程序的中間橋梁,不論是從properties文件讀取信息還是寫入信息到properties文件都要經由Properties類。

寫入

Properties類調用setProperty方法將鍵值對保存到內存中,此時可以通過getProperty方法讀取,propertyNames方法進行遍歷,但是并沒有將鍵值對持久化到屬性文件中,故需要調用store方法持久化鍵值對到屬性文件中。

我們寫一個類測試

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;

public class TestProperties {
	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("databases", "music_player");
			properties.store(output, "Steven1997 modify" + new Date().toString());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(output!=null) {
				try {
					output.close();
				}catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	public static void main(String[] args) {
		TestProperties t = new TestProperties();
		t.writeProperties();
	}
}

執行后,工程下面會出現一個config.properties文件,屬性文件內容如下:

如何在Java中使用Property類

讀取

使用getProperty獲取config.properties文件配置文件的各項屬性。

package property;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class LoadProperties {
	public void loadProperties() {
		Properties properties = new Properties();
		InputStream inputStream = null;
		
		try {
			inputStream = new FileInputStream("config.properties");
			properties.load(inputStream);
			System.out.println("url:" + properties.getProperty("url"));
			System.out.println("username:" + properties.getProperty("username"));
			System.out.println("password:" + properties.getProperty("password"));
			System.out.println("database:" + properties.getProperty("database"));
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(inputStream !=null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	public static void main(String[] args) {
		LoadProperties l = new LoadProperties();
		l.loadProperties();
	}
}

運行后的結果

url:jdbc:mysql://localhost:3306/
username:root
password:root
database:music_player

遍歷

遍歷屬性文件中的鍵值對

package property;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class PropertiesTest {
	public void printAll() {
		Properties prop = new Properties();
		InputStream input = null;
		try {
			String file = "config.properties";
			input = getClass().getClassLoader().getResourceAsStream(file);
			if(input == null) {
				System.out.println("無法加載文件" + file);
				return ;
			}
			prop.load(input);
			// 方法一
			Set<Object> keys = prop.keySet();
			for(Object key:keys) {
				System.out.println("key:" + key.toString() + "|" + "value:" + prop.get(key));
			}
			//方法二:
			Set<Entry<Object, Object>> entrys =	prop.entrySet();//返回的屬性鍵值對實體
			for(Entry<Object, Object> entry:entrys){
				System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
			}
			//方法三:
			Enumeration<?> e = prop.propertyNames();
			while (e.hasMoreElements()) {
				String key = (String) e.nextElement();
				String value = prop.getProperty(key);
				System.out.println("Key:" + key + ",Value:" + value);
			}

		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(input != null) {
				try {
					input.close();
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) {
		PropertiesTest p = new PropertiesTest();
		p.printAll();
	}
}

運行結果如下:

key:url|value:jdbc:mysql://localhost:3306/
key:password|value:root
key:database|value:music_player
key:username|value:root
key:url,value:jdbc:mysql://localhost:3306/
key:password,value:root
key:database,value:music_player
key:username,value:root
Key:password,Value:root
Key:url,Value:jdbc:mysql://localhost:3306/
Key:database,Value:music_player
Key:username,Value:root

java基本數據類型有哪些

Java的基本數據類型分為:1、整數類型,用來表示整數的數據類型。2、浮點類型,用來表示小數的數據類型。3、字符類型,字符類型的關鍵字是“char”。4、布爾類型,是表示邏輯值的基本數據類型。

上述內容就是如何在Java中使用Property類,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創新互聯行業資訊頻道。

文章名稱:如何在Java中使用Property類
分享路徑:http://vcdvsql.cn/article18/gjiodp.html

成都網站建設公司_創新互聯,為您提供手機網站建設網站收錄商城網站電子商務用戶體驗網站改版

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都定制網站網頁設計