java Properties集合與IO流
- Properties介紹
- 特有方法
- 示例代碼
- Properties和IO流相結合的方法
- 游戲次數案例【應用】
Properties介紹
- 是一個Map體系的集合類
- Properties可以保存到流中或從流中加載
- 屬性串列中的每個鍵及其對應的值都是一個字串
特有方法
-
方法名 說明 Object setProperty(String key, String value) 設定集合的鍵和值,都是String型別,底層呼叫 Hashtable方法 put String getProperty(String key) 使用此屬性串列中指定的鍵搜索屬性 Set stringPropertyNames() 從該屬性串列中回傳一個不可修改的鍵集,其中鍵及其對應的值是字串
示例代碼
public class PropertiesDemo02 {
public static void main(String[] args) {
//創建集合物件
Properties prop = new Properties();
//Object setProperty(String key, String value):設定集合的鍵和值,都是String型別,底層呼叫Hashtable方法put
prop.setProperty("itheima001", "林青霞");
/*
Object setProperty(String key, String value) {
return put(key, value);
}
Object put(Object key, Object value) {
return map.put(key, value);
}
*/
prop.setProperty("itheima002", "張曼玉");
prop.setProperty("itheima003", "王祖賢");
//String getProperty(String key):使用此屬性串列中指定的鍵搜索屬性
// System.out.println(prop.getProperty("itheima001"));
// System.out.println(prop.getProperty("itheima0011"));
// System.out.println(prop);
//Set<String> stringPropertyNames():從該屬性串列中回傳一個不可修改的鍵集,其中鍵及其對應的值是字串
Set<String> names = prop.stringPropertyNames();
for (String key : names) {
// System.out.println(key);
String value = prop.getProperty(key);
System.out.println(key + "," + value);
}
}
}
Properties和IO流相結合的方法
- 和IO流結合的方法
| 方法名 | 說明 |
|---|---|
| void load(InputStream inStream) | 從輸入位元組流讀取屬性串列(鍵和元素對) |
| void load(Reader reader) | 從輸入字符流讀取屬性串列(鍵和元素對) |
| void store(OutputStream out, String comments) | 將此屬性串列(鍵和元素對)寫入此 Properties表中,以適合于使用 load(InputStream)方法的格式寫入輸出位元組流 |
| void store(Writer writer, String comments) | 將此屬性串列(鍵和元素對)寫入此 Properties表中,以適合使用 load(Reader)方法的格式寫入輸出字符流 |
- 示例代碼
public class PropertiesDemo03 {
public static void main(String[] args) throws IOException {
//把集合中的資料保存到檔案
// myStore();
//把檔案中的資料加載到集合
myLoad();
}
private static void myLoad() throws IOException {
Properties prop = new Properties();
//void load(Reader reader):
FileReader fr = new FileReader("myOtherStream\\fw.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
private static void myStore() throws IOException {
Properties prop = new Properties();
prop.setProperty("itheima001","林青霞");
prop.setProperty("itheima002","張曼玉");
prop.setProperty("itheima003","王祖賢");
//void store(Writer writer, String comments):
FileWriter fw = new FileWriter("myOtherStream\\fw.txt");
prop.store(fw,null);
fw.close();
}
}
游戲次數案例【應用】
-
案例需求
- 實作猜數字小游戲只能試玩3次,如果還想玩,提示:游戲試玩已結束,想玩請充值(www.itcast.cn)
-
分析步驟
-
寫一個游戲類,里面有一個猜數字的小游戲
-
寫一個測驗類,測驗類中有main()方法,main()方法中寫如下代碼:
? 從檔案中讀取資料到Properties集合,用load()方法實作
- 檔案已經存在:game.txt
- 里面有一個資料值:count=0? 通過Properties集合獲取到玩游戲的次數
? 判斷次數是否到到3次了
-如果到了,給出提示:游戲試玩已結束,想玩請充值(www.itcast.cn)
-如果不到3次:次數+1,重新寫回檔案,用Properties的store()方法實作玩游戲
-
-
代碼實作
public class PropertiesTest {
public static void main(String[] args) throws IOException {
//從檔案中讀取資料到Properties集合,用load()方法實作
Properties prop = new Properties();
FileReader fr = new FileReader("myOtherStream\\game.txt");
prop.load(fr);
fr.close();
//通過Properties集合獲取到玩游戲的次數
String count = prop.getProperty("count");
int number = Integer.parseInt(count);
//判斷次數是否到到3次了
if(number >= 3) {
//如果到了,給出提示:游戲試玩已結束,想玩請充值(www.itcast.cn)
System.out.println("游戲試玩已結束,想玩請充值(www.itcast.cn)");
} else {
//玩游戲
GuessNumber.start();
//次數+1,重新寫回檔案,用Properties的store()方法實作
number++;
prop.setProperty("count",String.valueOf(number));
FileWriter fw = new FileWriter("myOtherStream\\game.txt");
prop.store(fw,null);
fw.close();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/296673.html
標籤:其他
上一篇:起訴18年,Linux 原始碼“抄襲”案以1425萬美元和解
下一篇:2021-8-31
