Java之Properties屬性操作
阿新 • • 發佈:2022-03-19
簡介:
在國際化程式的資原始檔(*.properties)中,資料是按照 Key=Value 的格式儲存的,而這種結構的儲存形式和Map集合很相似,但是唯一的區別在於其所儲存的內容只能夠是字串,那麼為了方便地描述屬性的定義,在java.util包裡面提供有一個Properties型別,此類是Hashtable的子類;
public class Properties
extends Hashtable<Object,Object>
可以發現繼承Hashtable的泛型定義是兩個Object型別,Properties不能操作泛型,只能操作String型別。
NO. |
方法名稱 |
型別 |
功能 |
1 |
public synchronized Object setProperty(String key, String value) { return put(key, value); } |
普通 | 設定屬性 |
2 |
public String getProperty(String key) { Object oval = map.get(key); String sval = (oval instanceof String) ? (String)oval : null; Properties defaults; |
普通 | 獲取屬性,不存在返回null |
3 |
public String getProperty(String key, String defaultValue) { String val = getProperty(key); return (val == null) ? defaultValue : val; } |
普通 |
獲取屬性,不存在返回預設值 |
4 |
public void store(OutputStream out, String comments) throws IOException { store0(new BufferedWriter(new OutputStreamWriter(out, ISO_8859_1.INSTANCE)), comments, true); } |
普通 | 輸出屬性內容 |
5 |
public void load(Properties props, InputStream in) throws IOException, InvalidPropertiesFormatException, UnsupportedEncodingException { this.properties = props; try { SAXParser parser = new SAXParserImpl(); parser.parse(in, this); } catch (SAXException saxe) { throw new InvalidPropertiesFormatException(saxe); } /** * String xmlVersion = propertiesElement.getAttribute("version"); if * (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0) throw new * InvalidPropertiesFormatException( "Exported Properties file format * version " + xmlVersion + " is not supported. This java installation * can read" + " versions " + EXTERNAL_XML_VERSION + " or older. You" + * " may need to install a newer version of JDK."); */ } |
普通 | 通過輸入流讀取屬性內容 |
程式碼實現:
import java.util.Hashtable;
import java.util.Properties;
public class MAIN {
public static void main(String[] args) {
Properties pop = new Properties();
// 設定只能是字串
pop.setProperty("one", "1");
pop.setProperty("on", "2");
pop.setProperty("o", "3");
System.out.println("通過Key檢視存在的Value: " + pop.getProperty("o")); // 通過Key檢視存在的Value
System.out.println("通過Key檢視存在的Value: " + pop.getProperty("on")); // 通過Key檢視存在的Value
System.out.println("通過Key檢視存在的Value: " + pop.getProperty("one")); // 通過Key檢視存在的Value
System.out.println("通過Key檢視不存在的Value: " + pop.getProperty("e")); // 通過Key檢視不存在的Value
System.out.println("設定預設值再通過Key檢視不存在的Value: " + pop.getProperty("e","Defualt")); // 設定預設值再通過Key檢視不存在的Value
}
}
輸出結果:
可以發現Properties裡面可以想Map集合那樣進行Key=Value的查詢,但是唯一的區別就是只能夠操作String型別
提供Properties的一個重要原因是:它可以通過輸出流輸出屬性,也可以通過輸入流讀取屬性內容,這是Map集合沒有的;
內容寫入操作:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Hashtable;
import java.util.Properties;
public class MAIN {
public static void main(String[] args) throws Exception{
Properties pop = new Properties();
// 設定只能是字串
pop.setProperty("one", "1");
pop.setProperty("on", "2");
pop.setProperty("o", "3");
pop.store(new FileOutputStream(new File("D:"+ File.separator + "Info.txt")), "中文 - English");
}
}
輸出結果:
文字中的中文內容會自動進行轉碼;
讀取操作:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Hashtable;
import java.util.Properties;
public class MAIN {
public static void main(String[] args) throws Exception{
Properties pop = new Properties();
pop.load(new FileInputStream(new File("D:"+ File.separator + "Info.txt")));
System.out.println(pop.getProperty("one"));
}
}
輸出結果
使用Properties最大的特點就是:進行資源內容的輸入與輸出操作;
實際情況中Properties一般用於讀取配置資原始檔的資訊,主要是在程式標準設計的初始化準備的時候使用。
Collections工具類 【JDK1.2】
Collections是Java提供的一組集合資料的操作工具類,利用它可以進行各個集合的操作。
public class Collections
操作關係:
從上面的操作關係可以發現,Collections類就是專門為集合進行各種資料操作而存在的工具類,使集合的資料操作更加便利,比如二分查詢,如果單獨實現是非常麻煩的,但是在Collections中提供有專門的方法,就只需要呼叫就行。
程式碼實現:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MAIN {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Collections.addAll(list,"Hello","World","China"); // 資料的新增
Collections.reverse(list); // 元素反轉
System.out.println(list);
System.out.println("World:" + Collections.binarySearch(list, "World")); // 二分查詢
}
}
輸出結果:
面試題:
請解釋Collection 與 Collections 的區別?.
- Collection是集合介面,允許儲存單值物件;
- Collections是集合操作的工具類。