Properties 類表示了一個持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每一個鍵及其對應值都是一個字符串。java
特色:ide
1、Hashtable的子類,map集合中的方法均可以用。工具
2、該集合沒有泛型。鍵值都是字符串。測試
3、它是一個能夠持久化的屬性集。鍵值能夠存儲到集合中,也能夠存儲到持久化的設備(硬盤、U盤、光盤)上。鍵值的來源也能夠是持久化的設備。ui
4、有和流技術相結合的方法。this
load(InputStream) 把指定流所對應的文件中的數據,讀取出來,保存到Propertie集合中spa
load(Reader) 操作系統
store(OutputStream,commonts)把集合中的數據,保存到指定的流所對應的文件中,參數commonts表明對描述信息code
stroe(Writer,comments);對象
代碼演示:
/* * * Properties集合,它是惟一一個能與IO流交互的集合 * * 需求:向Properties集合中添加元素,並遍歷 * * 方法: * public Object setProperty(String key, String value)調用 Hashtable 的方法 put。 * public Set<String> stringPropertyNames()返回此屬性列表中的鍵集, * public String getProperty(String key)用指定的鍵在此屬性列表中搜索屬性 */ public class PropertiesDemo01 { public static void main(String[] args) { //建立集合對象 Properties prop = new Properties(); //添加元素到集合 //prop.put(key, value); prop.setProperty("周迅", "張學友"); prop.setProperty("李小璐", "賈乃亮"); prop.setProperty("楊冪", "劉愷威"); //System.out.println(prop);//測試的使用 //遍歷集合 Set<String> keys = prop.stringPropertyNames(); for (String key : keys) { //經過鍵 找值 //prop.get(key) String value = prop.getProperty(key); System.out.println(key+"==" +value); } } }
需求:使用Properties集合,完成把集合內容存儲到IO流所對應文件中的操做
分析:
1,建立Properties集合
2,添加元素到集合
3,建立流
4,把集合中的數據存儲到流所對應的文件中
stroe(Writer,comments)
store(OutputStream,commonts)
把集合中的數據,保存到指定的流所對應的文件中,參數commonts表明對描述信息
5,關閉流
代碼演示:
public class PropertiesDemo02 { public static void main(String[] args) throws IOException { //1,建立Properties集合 Properties prop = new Properties(); //2,添加元素到集合 prop.setProperty("周迅", "張學友"); prop.setProperty("李小璐", "賈乃亮"); prop.setProperty("楊冪", "劉愷威"); //3,建立流 FileWriter out = new FileWriter("prop.properties"); //4,把集合中的數據存儲到流所對應的文件中 prop.store(out, "save data"); //5,關閉流 out.close(); } }
需求:從屬性集文件prop.properties 中取出數據,保存到集合中
分析:
1,建立集合
2,建立流對象
3,把流所對應文件中的數據 讀取到集合中
load(InputStream) 把指定流所對應的文件中的數據,讀取出來,保存到Propertie集合中
load(Reader)
4,關閉流
5,顯示集合中的數據
代碼演示:
public class PropertiesDemo03 { public static void main(String[] args) throws IOException { //1,建立集合 Properties prop = new Properties(); //2,建立流對象 FileInputStream in = new FileInputStream("prop.properties"); //FileReader in = new FileReader("prop.properties"); //3,把流所對應文件中的數據 讀取到集合中 prop.load(in); //4,關閉流 in.close(); //5,顯示集合中的數據 System.out.println(prop); } }
注意:使用字符流FileReader就能夠完成文件中的中文讀取操做了
用於從流中讀取對象的
操做流 ObjectInputStream 稱爲 反序列化流
用於向流中寫入對象的操做流 ObjectOutputStream 稱爲 序列化流
特色:用於操做對象。能夠將對象寫入到文件中,也能夠從文件中讀取對象。
ObjectOutputStream 將 Java 對象的基本數據類型和圖形寫入 OutputStream。可使用 ObjectInputStream 讀取(重構)對象。經過在流中使用文件能夠實現對象的持久存儲。
注意:只能將支持 java.io.Serializable 接口的對象寫入流中
代碼演示:
public class ObjectStreamDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { /* * 將一個對象存儲到持久化(硬盤)的設備上。 */ writeObj();//對象的序列化。 } public static void writeObj() throws IOException { //1,明確存儲對象的文件。 FileOutputStream fos = new FileOutputStream("tempfile\\obj.object"); //2,給操做文件對象加入寫入對象功能。 ObjectOutputStream oos = new ObjectOutputStream(fos); //3,調用了寫入對象的方法。 oos.writeObject(new Person("wangcai",20)); //關閉資源。 oos.close(); } }
Person類
public class Person implements Serializable { private String name; private int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
ObjectInputStream 對之前使用 ObjectOutputStream 寫入的基本數據和對象進行反序列化。支持 java.io.Serializable接口的對象才能從流讀取。
代碼演示
public class ObjectStreamDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { readObj();//對象的反序列化。 } public static void readObj() throws IOException, ClassNotFoundException { //1,定義流對象關聯存儲了對象文件。 FileInputStream fis = new FileInputStream("tempfile\\obj.object"); //2,創建用於讀取對象的功能對象。 ObjectInputStream ois = new ObjectInputStream(fis); Person obj = (Person)ois.readObject(); System.out.println(obj.toString()); } }
當一個對象要能被序列化,這個對象所屬的類必須實現Serializable接口。不然會發生異常NotSerializableException異常。
同時當反序列化對象時,若是對象所屬的class文件在序列化以後進行的修改,那麼進行反序列化也會發生異常InvalidClassException。發生這個異常的緣由以下:
該類的序列版本號與從流中讀取的類描述符的版本號不匹配
該類包含未知數據類型
該類沒有可訪問的無參數構造方法
Serializable標記接口。該接口給須要序列化的類,提供了一個序列版本號。serialVersionUID. 該版本號的目的在於驗證序列化的對象和對應類是否版本匹配。
代碼修改以下,修改後再次寫入對象,讀取對象測試
public class Person implements Serializable { //給類顯示聲明一個序列版本號。 private static final long serialVersionUID = 1L; private String name; private int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
當一個類的對象須要被序列化時,某些屬性不須要被序列化,這時不須要序列化的屬性可使用關鍵字transient修飾。只要被transient修飾了,序列化時這個屬性就不會琲序列化了。
同時靜態修飾也不會被序列化,由於序列化是把對象數據進行持久化存儲,而靜態的屬於類加載時的數據,不會被序列化。
代碼修改以下,修改後再次寫入對象,讀取對象測試
public class Person implements Serializable { /* * 給類顯示聲明一個序列版本號。 */ private static final long serialVersionUID = 1L; private static String name; private transient/*瞬態*/ int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
打印流添加輸出數據的功能,使它們可以方便地打印各類數據值表示形式.
打印流根據流的分類:
字節打印流 PrintStream
字符打印流 PrintWriter
方法:
void print(String str): 輸出任意類型的數據,
void println(String str): 輸出任意類型的數據,自動寫入換行操做
代碼演示:
/* * 需求:把指定的數據,寫入到printFile.txt文件中 * * 分析: * 1,建立流 * 2,寫數據 * 3,關閉流 */ public class PrintWriterDemo { public static void main(String[] args) throws IOException { //建立流 //PrintWriter out = new PrintWriter(new FileWriter("printFile.txt")); PrintWriter out = new PrintWriter("printFile.txt"); //2,寫數據 for (int i=0; i<5; i++) { out.println("helloWorld"); } //3,關閉流 out.close(); } }
能夠經過構造方法,完成文件數據的自動刷新功能
構造方法:
開啓文件自動刷新寫入功能
public PrintWriter(OutputStream out, boolean autoFlush)
public PrintWriter(Writer out, boolean autoFlush)
代碼演示:
/* * 分析: * 1,建立流 * 2,寫數據 */ public class PrintWriterDemo2 { public static void main(String[] args) throws IOException { //建立流 PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"), true); //2,寫數據 for (int i=0; i<5; i++) { out.println("helloWorld"); } //3,關閉流 out.close(); } }
加入classpath的第三方jar包內的class文件才能在項目中使用
建立lib文件夾
將commons-io.jar拷貝到lib文件夾
右鍵點擊commons-io.jar,Build Path→Add to Build Path
這個工具類是用來處理文件名(譯者注:包含文件路徑)的,他能夠輕鬆解決不一樣操做系統文件名稱規範不一樣的問題
經常使用方法:
getExtension(String path):獲取文件的擴展名;
getName():獲取文件名;
isExtension(String fileName,String ext):判斷fileName是不是ext後綴名;
提供文件操做(移動文件,讀取文件,檢查文件是否存在等等)的方法。
經常使用方法:
readFileToString(File file):讀取文件內容,並返回一個String;
writeStringToFile(File file,String content):將內容content寫入到file中;
copyDirectoryToDirectory(File srcDir,File destDir);文件夾複製
copyFile(File srcFile,File destFile);文件夾複製
代碼演示:
/* * 完成文件的複製 */ public class CommonsIODemo01 { public static void main(String[] args) throws IOException { //method1("D:\\test.avi", "D:\\copy.avi"); //經過Commons-IO完成了文件複製的功能 FileUtils.copyFile(new File("D:\\test.avi"), new File("D:\\copy.avi")); } //文件的複製 private static void method1(String src, String dest) throws IOException { //1,指定數據源 BufferedInputStream in = new BufferedInputStream(new FileInputStream(src)); //2,指定目的地 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); //3,讀 byte[] buffer = new byte[1024]; int len = -1; while ( (len = in.read(buffer)) != -1) { //4,寫 out.write(buffer, 0, len); } //5,關閉流 in.close(); out.close(); } } /* * 完成文件、文件夾的複製 */ public class CommonsIODemo02 { public static void main(String[] args) throws IOException { //經過Commons-IO完成了文件複製的功能 FileUtils.copyFile(new File("D:\\test.avi"), new File("D:\\copy.avi")); //經過Commons-IO完成了文件夾複製的功能 //D:\基礎班 複製到 C:\\abc文件夾下 FileUtils.copyDirectoryToDirectory(new File("D:\\基礎班"), new File("C:\\abc")); } }