Properties使用方法解析
{備註:最近除了出去找工作就是進行專案交接,偶爾靜下來研究一下以前不曾研究資訊,雖然都很簡單,但是當我們真的去寫一個demo對其結果檢視時,我們對該資訊的記憶瞬間變得更加深刻。}
在系統開發的過程中很多時候需要使用*.properties配置檔案,由於其具備與map<object,object>相似更增加了其在專案開發中的重要性。
可儲存資料如下作用:
1、用於儲存重要配置資訊如資料來源
2、使用者存放api介面資訊。
3、使用者儲存一些基本操作資訊。
一般在系統啟動時會自動載入配置檔案中德內容到快取中,而properties獨特的結構也為此提供的遍歷。對於專案中如何使用這裡暫不做說明。
public static void main(String[] args) {
Properties propert=new Properties();
try {
propert.load(PropertiesDemo.class.getResourceAsStream("user.properties"));//將properties檔案以資料流的方式匯入
boolean contains=propert.contains("資訊");//檢視是否包含指定內容
System.out.println(contains);
//------------------------遍歷檔案中的資料--------------------
Set<Entry<Object, Object>> sets= propert.entrySet();
Iterator<Entry<Object, Object>> it=sets.iterator();
while(it.hasNext()){
Entry<Object, Object> maps=it.next();
System.out.print(maps.getKey()+":"+maps.getValue()+" ");
}
System.out.println();
//------------------------獲取使用者名稱--------------------------
String username=(String)propert.get("name");
System.out.println("userName1="+username);
username=propert.getProperty("name");
System.out.println("userName2="+username);
//-----------------------遍歷鍵值key--------------------------
Set<Object> hashTbale=propert.keySet();
Iterator<Object> its=hashTbale.iterator();
while(its.hasNext()){
Object key=its.next();
System.out.print("key="+key+" ");
}
System.out.println();
//-----------------------遍歷key------------------------------
Enumeration<Object> enums=propert.keys();
while(enums.hasMoreElements()){
Object en=enums.nextElement();
System.out.print("en="+en+" ");
}
System.out.println();
//------------------------新增屬性----------------------------
propert.put("class", "二班");
int size=propert.size();
System.out.println("size1="+size);
propert.setProperty("bigdata", "superdata");
System.out.println("size2="+propert.size());
PrintWriter out=new PrintWriter(new FileOutputStream("userbak.properties"),true);
propert.list(out);
//---------------移除屬性-------------------------------------
Object object=propert.remove("name");
System.out.println("object="+object);
} catch (IOException e) {
e.printStackTrace();
}
}
還有一些直接將資料到位xml檔案的方法這裡不再做說明,祝工作順利。
相關文章
- gookit/properties - Java Properties 格式內容的解析,編碼解碼庫GoJava
- 深入解析JQuery中的isPlainObject()使用方法jQueryAIObject
- Binder的使用方法和原始碼解析原始碼
- css propertiesCSS
- Linux 記憶體使用方法詳細解析Linux記憶體
- Database_PropertiesDatabase
- properties外掛
- PHP中get_headers函式使用方法解析PHPHeader函式
- Java 對 properties 檔案操作 (ResourceBundle 類和 Properties 類)Java
- java.util.PropertiesJava
- druid.propertiesUI
- Java之Properties集合Java
- java Properties的用法Java
- Java遍歷PropertiesJava
- SpringBoot-PropertiesSpring Boot
- [XML與properties檔案]XML
- Properties屬性檔案
- database_properties 檢視Database
- Index Properties (209)Index
- 【Spring原始碼分析】.properties檔案讀取及佔位符${...}替換原始碼解析Spring原始碼
- PostgreSQL DBA(41) - PG Index PropertiesSQLIndex
- CSS Houdini: Properties, Values, and the Paint APICSSAIAPI
- properties檔案載入器
- Properties 持久的屬性集
- webwork.properties配置說明Web
- java讀取properties檔案Java
- JavaSE基礎:Properties屬性類Java
- MySQL-07——JDBC、Properties、DBCP、PreparedstatementMySqlJDBC
- properties標籤和typeAliases標籤
- 玩轉spring boot——properties配置Spring Boot
- properties檔案內容亂碼
- How to inject value into bean properties in SpringBeanSpring
- spring心得4--setter注入集合(set、list、map、properties等多種集合,配有案例解析)@基本裝Spring
- log4j.properties 配置檔案
- 堅定地使用 CSS Custom PropertiesCSS
- [java IO流]之 Properties屬性集Java
- spring引入properties變數報錯Spring變數
- mybatis讀取properties檔案內容MyBatis