JAVA基礎-IO流(二)
一、字節流
字節流是通過字節來進行讀寫操作的,他的使用對象相比於字符流來說更加的廣泛。這主要是因為他們讀寫文件的方
式而決定的。字符流讀寫文件時是將讀取到的字節通過默認編碼表轉換成字符,在通過默認編碼表將字符轉換成字節存儲
到文件裏,在這個過程中雖然提高了文本文檔的讀寫效率,但是在面對非文本文件時 - 字節轉換成字符的這個操作因為編
碼表的原因可能會造成丟失字節,導致文件的缺失。所以在對非文本文檔進行操作時要使用字節流。
二、InputStream類
此抽象類是字節輸入流的所有類的超類(讀),其常用子類有 FileInputStream、 ObjectInputStream 。
而此類的常用方法與Reader類相似,其常用方法有:
(1)關閉此輸入流並釋放與該流關聯的所有系統資源、close()
(2)從輸入流中讀取數據的下一個字節、 read()
(3)從輸入流中讀取一定數量的字節,並將其存儲在緩沖區數組 b 中、read(byte[] b)
(4)將輸入流中最多 len 個數據字節讀入 byte 數組、read(byte[] b, int off, int len)
1、FileInputStream類
FileInputStream類為InputStream抽象類的常用實現類,他可以用於讀取視頻、音頻、圖片等非文本文件,也可以
讀取文本文件(讀取文本文件推薦使用FileReader類),其構造方法與FileReader類似,可以傳遞路徑名或File對象為
參數。它繼承了InputStream類的全部方法。
2、ObjectInputStream類
ObjectInputStream類主要用於對象的讀取,繼承了InputStream類的全部方法。他的構造方法為:
(1)創建從指定 InputStream 讀取的 ObjectInputStream
ObjectInputStream(InputStream in)
因其讀取對象的特性,他有一些自己獨有的自己獨有的方法:
(1)從 ObjectInputStream 讀取對象、readObject()
兩種遍歷方式的舉例:
1 //拋出異常 2 private static void method2() throwsIOException, FileNotFoundException, ClassNotFoundException { 3 //根據傳入的FileInputStream對象創建ObjectInputStream對象 4 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("aa.txt")); 5 //遍歷文件直到無文件可讀,捕獲拋出的異常,結束循環 6 try { 7 while (true) { 8 //讀取文件 9 Object readObject = ois.readObject(); 10 System.out.println(readObject); 11 } 12 //捕獲異常結束循環 13 } catch (EOFException e) { 14 System.out.println("已經輸出完畢"); 15 //一定會執行,一般用於關閉流 16 } finally { 17 //關閉資源 18 ois.close(); 19 } 20 }
將對象傳入到集合中,將集合寫入文件,讀取集合
1 public static void main(String[] args) throws Exception, IOException { 2 // method(); 3 // method2(); 4 // 創建學生對象 5 Student student = new Student("張三", 18); 6 Student student2 = new Student("白潔", 19); 7 Student student3 = new Student("趙四", 39); 8 // 創建集合 9 ArrayList<Student> lists = new ArrayList<>(); 10 // 將學生丟向加入到集合中 11 lists.add(student); 12 lists.add(student2); 13 lists.add(student3); 14 // 根據傳入的FileOutputStream對象創建ObjectOutputStream對象 15 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("bb.txt")); 16 // 將集合寫入 17 oos.writeObject(lists); 18 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("bb.txt")); 19 // 從文件中讀取集合 20 Object readObject = ois.readObject(); 21 // 向下轉型 22 ArrayList<Student> re = (ArrayList<Student>) readObject; 23 // 遍歷集合 24 for (Student student4 : re) { 25 System.out.println(student4); 26 } 27 // 關閉資源 28 ois.close(); 29 oos.close();
三、OutputStream類
此抽象類是表示輸出字節流的所有類的超類(寫),其常用子類有 FileInputStream、 ObjectInputStream。
此類的常用方法與Reader類相似,其常用的實現類有:
(1)FileOutputStream類
(2)ObjectOutputStream類
而此類的常用方法與Writer類相似,其常用方法有:
(1)關閉此輸出流並釋放與此流有關的所有系統資源、close()
(2)刷新此輸出流並強制寫出所有緩沖的輸出字節、flush()
(3)將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此輸出流、write(byte[] b, int off, int len)
1、FileOutputStream類
FileOutputStream類為OutputStream抽象類的常用實現類,其構造方法與FileWriter類似,可以傳遞路徑名或
File對象為參數。它繼承了OutputStream類的全部方法。
用字符流拷貝文件舉例:
1 //創建File對象 2 File file = new File("D:\\songs\\小蘋果.mp4"); 3 //根據file創建FileInputStream對象 4 FileInputStream fi=new FileInputStream(file); 5 //定義int用來存數組長度 6 int i; 7 //定義byte數組 8 byte[] b=new byte[1024]; 9 //根據給定路徑創建FileOutputStream對象 10 FileOutputStream fo=new FileOutputStream("D:\\aa\\小蘋果.mp4"); 11 //循環讀取 12 while ((i=fi.read(b))!=-1) { 13 //寫入文件 14 fo.write(b, 0, i); 15 } 16 //關閉資源 17 fo.close(); 18 fi.close();
2、ObjectOutputStream類
可以用ObjectOutputStream類將對象寫入到文件中,要註意的是被寫入的對象的類要進行序列化,否則你在對類進行
修改時會報異常。
對類序列化舉例:
1 class Student implements Serializable { 2 /** 3 * 序列號如果不生成系統會自動生成,但是你更改類的成員變量序列號也會變,就會報錯所以要重寫 4 */ 5 //實現Serializable接口進行序列化 6 private static final long serialVersionUID = 4123259833120003092L; 7 private String name; 8 private int age; 9 10 public Student() { 11 super(); 12 // TODO Auto-generated constructor stub 13 } 14 15 public Student(String name, int age) { 16 super(); 17 this.name = name; 18 this.age = age; 19 } 20 21 @Override 22 public String toString() { 23 return "Student [name=" + name + ", age=" + age + "]"; 24 } 25 }
寫入對象舉例:
1 //創建ObjectOutputStream對象 2 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("aa.txt")); 3 //創建學生類 4 Student student = new Student("張三", 18); 5 Student student2 = new Student("白潔", 19); 6 //寫入數據 7 oos.writeObject(student); 8 oos.writeObject(student2); 9 //關閉資源 10 oos.close();
四、轉換流的使用
轉換流不屬於字節流的子類,而是Writer和Reader的子類,因為它涉及到了字節流和字符流的轉換所以在這裏說。
1、InputStreamReader(字符輸入流)
字符輸入流是字節流通向字符流的橋梁。它可以將節符流對象轉化成字符流對象,因為是輸入,所以字節數據會被轉
換成字符數據。其常用的構造方法為:InputStreamReader(InputStream in) 。
2、OutputStreamWriter(字符輸出流)
他是字符流通向字節流的橋梁。它可以將字節流對象轉換成字符流對象,因為是輸出,所以字符數據會被轉換成字節
數據。其常用的構造方法為:創建使用默認字符編碼的 - OutputStreamWriter(OutputStream out)。
舉例:
1 public static void main(String[] args) throws IOException { 2 // 標準輸入流 3 InputStream in = System.in; 4 // 轉換流 5 InputStreamReader inputStreamReader = new InputStreamReader(in); 6 // 創建FileWriter對象 7 FileWriter fw = new FileWriter("a.txt"); 8 // 存儲數組長度 9 int i; 10 char[] c = new char[1024]; 11 // 循環讀取 12 while ((i = inputStreamReader.read(c)) != -1) { 13 // 將數組寫入到指定文件,從0一直寫到i,目的是為了防止出現重復數據 14 fw.write(c, 0, i); 15 // 刷 16 fw.flush(); 17 18 } 19 // 關閉資源 20 fw.close(); 21 inputStreamReader.close(); 22 } 23 }
JAVA基礎-IO流(二)