標籤:pre row oda pack dex 寫代碼 lin 特點 pat
一、Java Properties類
Java中有個比較重要的類Properties(Java.util.Properties),主要用於讀取Java的設定檔,各種語言都有自己所支援的設定檔,設定檔中很多變數是經常改變的,這樣做也是為了方便使用者,讓使用者能夠脫離程式本身去修改相關的變數設定。像Python支援的設定檔是.ini檔案,同樣,它也有自己讀取設定檔的類ConfigParse,方便程式員或使用者通過該類的方法來修改.ini設定檔。在Java中,其設定檔常為.properties檔案,格式為文字檔,檔案的內容的格式是“鍵=值”的格式,文本注釋資訊可以用"#"來注釋。
Properties類繼承自Hashtable,如下:
它提供了幾個主要的方法:
1. getProperty ( String key),用指定的鍵在此屬性列表中搜尋屬性。也就是通過參數 key ,得到 key 所對應的 value。
2. load ( InputStream inStream),從輸入資料流中讀取屬性列表(鍵和元素對)。通過對指定的檔案(比如說上面的 test.properties 檔案)進行裝載來擷取該檔案中的所有鍵 - 值對。以供 getProperty ( String key) 來搜尋。
3. setProperty ( String key, String value) ,調用 Hashtable 的方法 put 。他通過調用基類的put方法來設定 鍵 - 值對。
4. store ( OutputStream out, String comments),以適合使用 load 方法載入到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出資料流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的檔案中去。
5. clear (),清除所有裝載的 鍵 - 值對。該方法在基類中提供。
二、Java讀取Properties檔案
最常用的還是通過java.lang.Class類的getResourceAsStream(String name)方法來實現,如下可以這樣調用:
InputStream in = getClass().getResourceAsStream("資源Name");作為我們寫程式的,用此一種足夠。
或者下面這種也常用:
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
三、課堂練習
1.
package day042301;import java.util.Properties;import java.util.Set;public class demo1 { public static void main(String[] args) { Properties pro=new Properties(); pro.setProperty("a", "1"); pro.setProperty("b", "2"); pro.setProperty("c", "3"); pro.setProperty("d", "4"); String vl=pro.getProperty("d"); System.out.println(vl); //遍曆集合,擷取到裝有索引值的set集合 Set<String> s=pro.stringPropertyNames(); for(String str:s){ //通過遍曆鍵擷取對應的值 String key=str; String value=pro.getProperty(key); System.out.println(key+"..."+value); } }}
運行結果
2.
package day042301;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Iterator;import java.util.Properties;import java.util.Set;public class demo2 { public static void main(String[] args) throws IOException { input(); }public static void output() throws IOException{ Properties pro=new Properties(); pro.setProperty("李寶林一號", "吃外賣"); pro.setProperty("李寶林二號", "吃大餐"); pro.setProperty("李寶林三號", "吃零食"); FileWriter fw=new FileWriter("d:\\test\\demo.properties"); pro.store(fw, ""); fw.close();}//輸出public static void input() throws IOException{ Properties pro=new Properties(); FileReader fr=new FileReader("d:\\test\\demo.properties"); pro.load(fr); fr.close(); Set<String> set=pro.stringPropertyNames(); Iterator<String> it=set.iterator(); while(it.hasNext()){ String key=it.next(); String value=pro.getProperty(key); System.out.println(key+"..."+value); }}}
運行結果
3.
package day042303;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.commons.io.FilenameUtils;public class fileutildemo { public static void main(String[] args) throws IOException { filenameutil();//擷取副檔名 } public static void filenameutil() throws IOException{ FilenameUtils fnu=new FilenameUtils(); //擷取副檔名 String name1=fnu.getExtension("d:\\test\\person.txt"); //擷取檔案名稱 String name2=fnu.getName("d:\\test\\person.txt"); //判斷尾碼名 boolean flag=fnu.isExtension("d:\\test\\person.txt", "txt"); System.out.println(name1+" "+name2+" "+flag); //讀取檔案內容 FileUtils fu=new FileUtils(); String content=fu.readFileToString(new File("d:\\test\\person.txt")); System.out.println(content); //fu.writeStringToFile(new File("d:\\test\\today.txt"), "今天下雨了"); //fu.copyDirectoryToDirectory(new File("d:\\test"), new File("d:\\java")); //複製檔案 fu.copyFile(new File("d:\\test\\today.txt"), new File("d:\\java\\ttttt.txt")); }}
運行結果
4.
package day042303;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;/*列印流 * 1.printwriter(file,output,string,writer)(自動重新整理),靈活性更強 * *2.printstream * 特點: * 1.該流不負責資料來源,只負責資料目的 *2.為其他輸出資料流添加功能 *3.永遠不拋IOException */public class printdemo { public static void main(String[] args) throws IOException { copy(); } public static void method1() throws FileNotFoundException{ File file=new File("d:\\test\\person.txt"); PrintWriter pw=new PrintWriter(file); pw.println("100"); pw.flush(); pw.write(100); pw.close(); } public static void method2(){ int[] arr={1}; System.out.println(arr); char[] ch={‘a‘,‘b‘,‘c‘}; System.out.println(ch); } public static void method3() throws FileNotFoundException{ //列印流的自動重新整理功能 //條件:1.你的輸出資料目的必須是流對象 //2.必須調用的方法:println,printf,format其中一個 File file=new File("d:\\test\\person.txt"); FileOutputStream fos=new FileOutputStream(file); PrintWriter pw=new PrintWriter(fos,true); pw.println("五一去哪玩"); pw.println("第一天去哪玩"); pw.println("寫代碼啊"); pw.close(); }//複製 public static void copy() throws IOException{ //讀取資料 用budderedreader+filereader //寫入目的地:printwriter+println自動重新整理功能 BufferedReader br=new BufferedReader(new FileReader("d:\\test\\person.txt")); PrintWriter pw=new PrintWriter(new FileWriter("d:\\test\\aaaa\\hello.txt"),true); String line=null; while((line=br.readLine())!=null){ pw.println(line); } pw.close(); br.close(); }}
2018年4月23日JAVA