package Properties; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.time.format.TextStyle; import java.util.Properties; import java.util.Set; /** * Map * --|Hashtable * --|Properties * Properties集合特点: * 1、该集合中的键和值都是字符串类型。 * 2、集合中的数据可以保存到流中,或者从流中获取数据。 * * 通常该集合用于操作以键值对形式存在的配置文件。 * */ public class PropertiesDemo_1 { public static void main(String[] args) throws IOException { //methodDemo_4(); myLoad(); test(); } public static void test() throws IOException { //讀取文件 File file = new File("info.txt"); if(!file.exists()){ file.createNewFile(); } FileReader fr = new FileReader(file); //創建集合存儲配置信息 Properties prop = new Properties(); //將流中信息存儲到集合中。 prop.load(fr); prop.setProperty("zhangsan", "16"); FileWriter fw1 = new FileWriter(file); prop.store(fw1, ""); fr.close(); fw1.close(); } /** * load方法原理: * */ public static void myLoad() throws IOException { Properties prop = new Properties(); BufferedReader bufr = new BufferedReader(new FileReader("info.txt") ); String line = null; while((line = bufr.readLine())!=null){ if(line.startsWith("#")){ continue; } String[]arr = line.split("="); System.out.println(arr[0]+"::"+arr[1]); } bufr.close(); } public static void methodDemo_4() throws IOException { Properties prop = new Properties(); FileInputStream fis = new FileInputStream("info.txt"); prop.load(fis); fis.close(); } public static void methodDemo_3() throws IOException { //创建一个Properties集合 Properties prop = new Properties(); //存储元素。 prop.setProperty("zhangsan", "34"); prop.setProperty("lisi", "33"); prop.setProperty("tom", "32"); //想要將這些集合中的字符串鍵值對信息持久化存儲到文件中,就要關聯輸出流; FileOutputStream fos = new FileOutputStream("info.txt"); prop.store(fos,"name and age"); fos.close(); } /** * 演示Properties集合和流对象相结合的功能 * */ public static void methodDemo_2(){ Properties prop = new Properties(); // prop.setProperty("zhangsan", "34"); // prop.setProperty("lisi", "33"); // prop.setProperty("tom", "32"); prop=System.getProperties(); prop.list(System.out); } /** * Properties集合的存和取。 * */ public static void propertiesDemo(){ //创建一个Properties集合 Properties prop = new Properties(); //存储元素。 prop.setProperty("zhangsan", "34"); prop.setProperty("lisi", "33"); prop.setProperty("tom", "32"); //修改元素 prop.setProperty("tom", "22"); //取出所有元素 Set<String>names = prop.stringPropertyNames(); for(String name:names){ String value = prop.getProperty(name); System.out.println(name+"::"+value); } } }
package Properties; /** * 配置文件應用:Properties 與IO技術相關,方便操作硬盤等設備;應用與配置文件,而軟件的配置文件可以用來配置軟件的個性化設置,以及需要長久保存的內容,都可以考慮保存到配置文件中; * */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; /** * 需求:定義一個功能,記錄程序運行次數;當程序運行5次,停止使用給出提示請付費購買正版; * 思路: * 1、應該有計數器 * 2、程序啟動時進行計數,計數器就必須存在內存中並進行運算,運算完畢計數變量消失,再重新啟動程序變量重新加載,達不到計數效果; * 而我們需要多次啟動同一個應用程序,這就需要計數器的生命週期邊長,從內存存儲到硬盤文件中 * 3、程序啟動時先讀取用於讀取記錄計數器的配置文件,獲取上一次計數次數判斷次數,并進行自增,自增后重新存儲到配置文件中; * 4、文件中信息如何進行存儲并進行體現呢?直接存儲此數值可以,但是不明確搞數據的含義,所以起名字就變得很重要; * 這就有了鍵值對關係,並且還要關聯IO,所以可以使用map集合中的Properties集合;如果更複雜可以使用xml配置文件; * */ public class PropertiesTest { public static void main(String[] args) throws IOException { getAppCount(); } public static void getAppCount() throws IOException { //將配置文件封裝成對象 File confile = new File("count.properties"); if(!confile.exists()){ confile.createNewFile(); } FileInputStream fis1 = new FileInputStream(confile); Properties prop = new Properties(); prop.load(fis1); //從集合中通過鍵獲取次數 String value = prop.getProperty("time"); //定義計數器 int count=0; if(value!=null){ count= Integer.parseInt(value); if(count>=5){ throw new RuntimeException("試用次數已到,請付費購買!"); } } count++; //改變后的次數重新記錄到集合中 prop.setProperty("time", count+""); FileOutputStream fos1 = new FileOutputStream(confile); prop.store(fos1, ""); fos1.close(); fis1.close(); } }