標籤:java
Properties:
Hashtable的子類,屬於集合類。儲存屬性累心公的索引值對, 鍵和值預設都是String 是集合中可以和流結合使用的一個集合類
Properties pro = new Properties();
pro.setProperty(“name”, “Tom”);
pro.setProperty(“age”, “19”);
pro.setProperty(“sex”, “boy”);
System.out.println(pro);
// 獲得所有屬性名稱的集合 Set<String> names = pro.stringPropertyNames(); Iterator<String> it = names.iterator(); while (it.hasNext()) { String key = it.next(); String value = pro.getProperty(key); System.out.println(key + "----" + value); }
與檔案流結合使用:
通過Properties類載入.properties檔案,並讀取資訊
1 流 2 建立Properties對象,載入屬性檔案 3 通過Properties對象的方法,將資訊讀取出來
// 寫入properties檔案
static void writeProperties(File file, Map
資料流:專門用來寫基本類型資料,讀和寫的順序嚴格對應
DataInputStream(InputStream )
readInt()
readDouble()
readUTF()
DataOutputStream(OutputStream)
writeInt()
writeDouble()
writeUTF()
flush()
// 建立一個資料流
DataOutputStream os = new DataOutputStream(new FileOutputStream(
“src/data.txt”));
os.writeDouble(12.2);
os.writeInt(10);
os.writeUTF(“好好學習”);
os.flush();
// 讀 DataInputStream is = new DataInputStream(new FileInputStream( "src/data.txt")); System.out.println(is.readDouble()); System.out.println(is.readInt()); System.out.println(is.readUTF());
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Java基礎 筆記(七)