IO流(Properties的使用方法)
阿新 • • 發佈:2022-04-04
* Properties:屬性集合類。是一個可以和IO流相結合使用的集合類。
* Properties 可儲存在流中或從流中載入。屬性列表中每個鍵及其對應值都是一個字串。
package cn.itcast_08; import java.util.Properties; import java.util.Set; /* * Properties:屬性集合類。是一個可以和IO流相結合使用的集合類。 * Properties 可儲存在流中或從流中載入。屬性列表中每個鍵及其對應值都是一個字串。 * * 是Hashtable的子類,說明是一個Map集合。 */ public classPropertiesDemo { public static void main(String[] args) { // 作為Map集合的使用 // 下面這種用法是錯誤的,一定要看API,如果沒有<>,就說明該類不是一個泛型類,在使用的時候就不能加泛型 // Properties<String, String> prop = new Properties<String, String>(); Properties prop = new Properties(); // 新增元素 prop.put("it002", "hello"); prop.put("it001", "world"); prop.put("it003", "java"); // System.out.println("prop:" + prop); // 遍歷集合 Set<Object> set = prop.keySet(); for (Object key : set) { Object value = prop.get(key); System.out.println(key + "---" + value); } } }
/*
* 特殊功能:
* public Object setProperty(String key,String value):新增元素
* public String getProperty(String key):獲取元素
* public Set<String> stringPropertyNames():獲取所有的鍵的集合
*/
package cn.itcast_08; import java.util.Properties; import java.util.Set; /* * 特殊功能: * public Object setProperty(String key,String value):新增元素 * public String getProperty(String key):獲取元素 * public Set<String> stringPropertyNames():獲取所有的鍵的集合 */ public class PropertiesDemo2 { public static void main(String[] args) { // 建立集合物件 Properties prop = new Properties(); // 新增元素 prop.setProperty("張三", "30"); prop.setProperty("李四", "40"); prop.setProperty("王五", "50"); // public Set<String> stringPropertyNames():獲取所有的鍵的集合 Set<String> set = prop.stringPropertyNames(); for (String key : set) { String value = prop.getProperty(key); System.out.println(key + "---" + value); } } } /* * class Hashtalbe<K,V> { public V put(K key,V value) { ... } } * * class Properties extends Hashtable { public V setProperty(String key,String * value) { return put(key,value); } } */
這裡的集合必須是Properties集合:
* public void load(Reader reader):把檔案中的資料讀取到集合中
* public void store(Writer writer,String comments):把集合中的資料儲存到檔案
package cn.itcast_08; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.util.Properties; /* * 這裡的集合必須是Properties集合: * public void load(Reader reader):把檔案中的資料讀取到集合中 * public void store(Writer writer,String comments):把集合中的資料儲存到檔案 * * 單機版遊戲: * 進度儲存和載入。 * 三國群英傳,三國志,仙劍奇俠傳... * * 呂布=1 * 方天畫戟=1 */ public class PropertiesDemo3 { public static void main(String[] args) throws IOException { // myLoad(); myStore(); } private static void myStore() throws IOException { // 建立集合物件 Properties prop = new Properties(); prop.setProperty("林青霞", "27"); prop.setProperty("武鑫", "30"); prop.setProperty("劉曉曲", "18"); //public void store(Writer writer,String comments):把集合中的資料儲存到檔案 Writer w = new FileWriter("name.txt"); prop.store(w, "helloworld"); w.close(); } private static void myLoad() throws IOException { Properties prop = new Properties(); // public void load(Reader reader):把檔案中的資料讀取到集合中 // 注意:這個檔案的資料必須是鍵值對形式 Reader r = new FileReader("prop.txt"); prop.load(r); r.close(); System.out.println("prop:" + prop); } }
判斷檔案是否有指定的鍵,如果有就修該其值
練習版
package Day22; import java.io.*; import java.util.Properties; import java.util.Set; /* * 我有一個文字檔案(user.txt),我知道資料是鍵值對形式的,但是不知道內容是什麼。 * 請寫一個程式判斷是否有“lisi”這樣的鍵存在,如果有就改變其實為”100” * * 分析: * A:把檔案中的資料載入到集合中 * B:遍歷集合,獲取得到每一個鍵 * C:判斷鍵是否有為"lisi"的,如果有就修改其值為"100" * D:把集合中的資料重新儲存到檔案中 */ public class PropertiesTest { public static void main(String[] args) throws IOException { //把檔案中的資料載入到集合中 //建立集合物件 Properties ppt = new Properties(); //給出要讀取的檔案 Reader w = new FileReader("zhao.txt"); //* public void load(Reader reader):把檔案中的資料讀取到集合中 ppt.load(w); //釋放資源 w.close(); //遍歷集合獲取每一個鍵 //public Set<String> stringPropertyNames():獲取所有的鍵的集合 //返回值==接受型別是Set<String> Set<String> set = ppt.stringPropertyNames(); for(String e: set){ // 判斷鍵是否有為"lisi"的,如果有就修改其值為"100" if ("lisi".equals(e)){ //* public Object setProperty(String key,String value):新增元素 ppt.setProperty(e,"100"); break; } } //把集合中的資料重新儲存到檔案中 //給出要新增的元素的檔案 Writer m = new FileWriter("zhao.txt"); //* public void store(Writer writer,String comments):把集合中的資料儲存到檔案 ppt.store(m,null); w.close(); } }
標準版
package cn.itcast_08; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.util.Properties; import java.util.Set; /* * 我有一個文字檔案(user.txt),我知道資料是鍵值對形式的,但是不知道內容是什麼。 * 請寫一個程式判斷是否有“lisi”這樣的鍵存在,如果有就改變其實為”100” * * 分析: * A:把檔案中的資料載入到集合中 * B:遍歷集合,獲取得到每一個鍵 * C:判斷鍵是否有為"lisi"的,如果有就修改其值為"100" * D:把集合中的資料重新儲存到檔案中 */ public class PropertiesTest { public static void main(String[] args) throws IOException { // 把檔案中的資料載入到集合中 Properties prop = new Properties(); Reader r = new FileReader("user.txt"); prop.load(r); r.close(); // 遍歷集合,獲取得到每一個鍵 Set<String> set = prop.stringPropertyNames(); for (String key : set) { // 判斷鍵是否有為"lisi"的,如果有就修改其值為"100" if ("lisi".equals(key)) { prop.setProperty(key, "100"); break; } } // 把集合中的資料重新儲存到檔案中 Writer w = new FileWriter("user.txt"); prop.store(w, null); w.close(); } }
如何讓猜數字遊戲只玩5次
package cn.itcast_08; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.util.Properties; /* * 我有一個猜數字小遊戲的程式,請寫一個程式實現在測試類中只能用5次,超過5次提示:遊戲試玩已結束,請付費。 */ public class PropertiesTest2 { public static void main(String[] args) throws IOException { // 讀取某個地方的資料,如果次數不大於5,可以繼續玩。否則就提示"遊戲試玩已結束,請付費。" // 建立一個檔案 // File file = new File("count.txt"); // if (!file.exists()) { // file.createNewFile(); // } // 把資料載入到集合中 Properties prop = new Properties(); Reader r = new FileReader("count.txt"); prop.load(r); r.close(); // 我自己的程式,我當然知道里面的鍵是誰 String value = prop.getProperty("count"); int number = Integer.parseInt(value); if (number > 5) { System.out.println("遊戲試玩已結束,請付費。"); System.exit(0); } else { number++; prop.setProperty("count", String.valueOf(number)); Writer w = new FileWriter("count.txt"); prop.store(w, null); w.close(); GuessNumber.start(); } } }
nio包在JDK4出現,提供了IO流的操作效率。但是目前還不是大範圍的使用。
* 有空的話瞭解下,有問題再問我。
package cn.itcast_09; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; /* * nio包在JDK4出現,提供了IO流的操作效率。但是目前還不是大範圍的使用。 * 有空的話瞭解下,有問題再問我。 * * JDK7的之後的nio: * Path:路徑 * Paths:有一個靜態方法返回一個路徑 * public static Path get(URI uri) * Files:提供了靜態方法供我們使用 * public static long copy(Path source,OutputStream out):複製檔案 * public static Path write(Path path,Iterable<? extends CharSequence> lines,Charset cs,OpenOption... options) */ public class NIODemo { public static void main(String[] args) throws IOException { // public static long copy(Path source,OutputStream out) // Files.copy(Paths.get("ByteArrayStreamDemo.java"), new // FileOutputStream( // "Copy.java")); ArrayList<String> array = new ArrayList<String>(); array.add("hello"); array.add("world"); array.add("java"); Files.write(Paths.get("array.txt"), array, Charset.forName("GBK")); } }