內容介紹
-
Properties集合
-
序列化流與反序列化流
-
打印流
-
commons-IO
Properties類
Properties類介紹
Properties 類表示了一個持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每個鍵及其對應值都是一個字符串。
特點:
1、Hashtable的子類,map集合中的方法都可以用。
2、該集合沒有泛型。鍵值都是字符串。
3、它是一個可以持久化的屬性集。鍵值可以存儲到集合中,也可以存儲到持久化的設備(硬盤、U盤、光盤)上。鍵值的來源也可以是持久化的設備。
4、有和流技術相結合的方法。
-
load(InputStream) 把指定流所對應的文件中的數據,讀取出來,保存到Propertie集合中
-
load(Reader)
-
store(OutputStream,commonts)把集合中的數據,保存到指定的流所對應的文件中,參數commonts代表對描述信息
-
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
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
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修飾。只要被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(); } }
commons-IO
導入classpath
加入classpath的第三方jar包內的class文件才能在項目中使用
創建lib文件夾
將commons-io.jar拷貝到lib文件夾
右鍵點擊commons-io.jar,Build Path→Add to Build Path
FilenameUtils
這個工具類是用來處理文件名(譯者注:包含文件路徑)的,他可以輕鬆解決不同操作系統文件名稱規範不同的問題
-
常用方法:
getExtension(String path):獲取文件的擴展名;
getName():獲取文件名;
isExtension(String fileName,String ext):判斷fileName是否是ext後綴名;
FileUtils
提供文件操作(移動文件,讀取文件,檢查文件是否存在等等)的方法。
-
常用方法:
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")); } }
總結
-
IO流總結
-
字節流
-
字節輸入流 InputStream
-
FileInputStream 操作文件的字節輸入流
-
BufferedInputStream高效的字節輸入流
-
ObjectInputStream 反序列化流
-
-
字節輸出流 OutputStram
-
FileOutputStream 操作文件的字節輸出流
-
BufferedOutputStream 高效的字節輸出流
-
ObjectOuputStream 序列化流
-
PrintStream 字節打印流
-
-
-
字符流
-
字符輸入流 Reader
-
FileReader 操作文件的字符輸入流
-
BufferedReader 高效的字符輸入流
-
InputStreamReader 輸入操作的轉換流(把字節流封裝成字符流)
-
-
字符輸出流 Writer
-
FileWriter 操作文件的字符輸出流
-
BufferedWriter 高效的字符輸出流
-
OutputStreamWriter 輸出操作的轉換流(把字節流封裝成字符流)
-
PrintWriter 字符打印流
-
-
-
方法:
-
讀數據方法:
-
read() 一次讀一個字節或字符的方法
-
read(byte[] char[]) 一次讀一個數組數據的方法
-
readLine() 一次讀一行字符串的方法(BufferedReader類特有方法)
-
readObject() 從流中讀取對象(ObjectInputStream特有方法)
-
-
寫數據方法:
-
write(int) 一次寫一個字節或字符到文件中
-
write(byte[] char[]) 一次寫一個數組數據到文件中
-
write(String) 一次寫一個字符串內容到文件中
-
writeObject(Object ) 寫對象到流中(ObjectOutputStream類特有方法)
-
newLine() 寫一個換行符號(BufferedWriter類特有方法)
-
-
-
向文件中寫入數據的過程
1,創建輸出流對象
2,寫數據到文件
3,關閉輸出流
-
從文件中讀數據的過程
-
創建輸入流對象
-
從文件中讀數據
-
關閉輸入流
-
文件複製的過程
-
創建輸入流(數據源)
-
創建輸出流(目的地)
-
從輸入流中讀數據
-
通過輸出流,把數據寫入目的地
-
關閉流
-
File類
-
方法
-
獲取文件名稱 getName()
-
獲取文件絕對路徑 getAbsolutePath()
-
獲取文件大小 length()
-
獲取當前文件夾中所有File對象 File[] listFiles()
-
判斷是否爲文件 isFile()
-
判斷是否爲文件夾 isDirectory()
-
創建文件夾 mkdir() mkdirs()
-
創建文件 createNewFile()
-
-
-
異常
-
try..catch…finally捕獲處理異常
-
throws 聲明異常
-
throw 拋出異常對象
-
-
異常的分類
-
編譯期異常 Exception
|- 運行期異常 RuntimeException
-
-
注意:
編譯期異常,必須處理,不然無法編譯通過
運行期異常,程序運行過程中,產生的異常信息
-
Properties:Map集合的一種,它是Hashtable集合的子集合,它鍵與值都是String類型,它是唯一能與IO流結合使用的集合
-
方法
-
load( InputStream in ) 從流所對應的文件中,讀數據到集合中
-
load( Reader in ) 從流所對應的文件中,讀數據到集合中
-
store( OutputStream out , String message ) 把集合中的數據,寫入到流所對應的文件中
-
store( Writer out , String message) 把集合中的數據,寫入到流所對應的文件中
-
-
-
實現文件內容的自動追加
-
構造方法
-
FileOutputStream(File file, boolean append)
-
FileOutputStream(String fileName, boolean append)
-
FileWriter(File, boolean append)
-
FileWriter(String fileName, boolean append)
-
-
實現文件內容的自動刷新
-
構造方法
-
PrintStream(OutputStream out, boolean autoFlush)
-
PrintWriter(Writer out, boolean autoFlush)
-
-
Commons-IO
-
方法
-
readFileToString(File file):讀取文件內容,並返回一個String;
-
writeStringToFile(File file,String content):將內容content寫入到file中;
-
copyDirectoryToDirectory(File srcDir,File destDir);文件夾複製
-
copyFileToDirectory (File srcFile,File destFile);文件複製
-