快樂來的越容易,痛苦也就會來的越頻繁。java
若是沒有互聯網,也許你這輩子真就受困於你身邊圈子的認知了。面試
序列流的存在價值是:幫助整合多個文件的內容放到一個文件中。數組
整合多個時,須要使用枚舉的方式。優化
package com.test.demo001; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.SequenceInputStream; import java.util.Enumeration; import java.util.Vector; public class Demo010 { public static void main(String[] args) throws IOException { //demo01(); //demo02(); demo03(); } private static void demo03() throws FileNotFoundException, IOException { FileInputStream fis1 = new FileInputStream("a.txt"); FileInputStream fis2 = new FileInputStream("b.txt"); FileInputStream fis3 = new FileInputStream("c.txt"); Vector<InputStream> v = new Vector<>(); //建立Vector集合對象 v.add(fis1); v.add(fis2); v.add(fis3); Enumeration<InputStream> en = v.elements();//獲取枚舉引用 SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("o.txt"); int a; while((a = sis.read())!= -1){ fos.write(a); } sis.close(); fos.close(); } private static void demo02() throws FileNotFoundException, IOException { FileInputStream fis1 = new FileInputStream("a.txt"); FileInputStream fis2 = new FileInputStream("b.txt"); FileInputStream fis3 = new FileInputStream("c.txt"); SequenceInputStream sis = new SequenceInputStream(fis1,fis2);//只能整合兩個流對象 FileOutputStream fos = new FileOutputStream("o.txt"); int a; while((a = sis.read())!= -1){ fos.write(a); } sis.close(); fos.close(); } private static void demo01() throws FileNotFoundException, IOException { FileInputStream fis1 = new FileInputStream("a.txt"); FileOutputStream fos = new FileOutputStream("o.txt"); int a; while((a = fis1.read())!= -1){ fos.write(a); } fis1.close(); FileInputStream fis2 = new FileInputStream("b.txt"); int b; while((b = fis2.read())!= -1){ fos.write(b); } fis2.close(); FileInputStream fis3 = new FileInputStream("c.txt"); int c; while((c = fis3.read())!= -1){ fos.write(c); } fis3.close(); fos.close(); } }
該輸出流能夠向內存中寫入數據,它是把內存做爲一個緩衝區,寫出以後一次性將數據取出,經常使用於聊天應用。編碼
數組和集合在內存中用完就自動釋放了,不像流,跟硬盤有關聯,須要手動關閉。code
建立對象:new ByteArrayOutputStream()對象
寫出數據:write(int),write(byte[])接口
獲取數據:toByteArray()遊戲
private static void demo01() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("a.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream();//在內存中建立了能夠增加的內存數組 int a; while((a = fis.read())!= -1){ baos.write(a); //將讀取到的數據逐個寫到內存中 } fis.close(); //這種方式能夠指定轉換數組的編碼 // byte[] arr = baos.toByteArray(); //將緩衝區的數據所有獲取出來,並賦值給arr數組 // System.out.println(new String(arr)); //這種方式使用的是平臺默認的編碼表 System.out.println(baos); }
FileInputStream讀取中文時出現亂碼的解決方案:內存
字符流
內存輸出流:ByteArrayOutputStream
package com.test.demo001; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; public class Demo010 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("d.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] arr = new byte[5]; int len; while((len = fis.read(arr))!= -1){ baos.write(arr,0,len); } System.out.println(baos); fis.close(); } }
序列化:將對象寫到文件上【相似遊戲存檔】
反序列化:將對象從文件中讀取出來【相似遊戲讀檔】
對象寫到文件上會是亂碼,但木有關係,能讀出來就行。
對象必須是可序列化的,即它必須實現Serializeable接口
Serializeable接口中沒有任何方法,它就是一個標記而已
若本身不指定,則類的ID號是隨機的
package com.test.demo001; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Demo010 { public static void main(String[] args) throws IOException { Student s1 = new Student("z3",23); Student s2 = new Student("l4",24); FileOutputStream fos = new FileOutputStream("k.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(s1); oos.writeObject(s2); fos.close(); oos.close(); } }
package com.test.demo001; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; public class Demo011 { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("k.txt")); Student s1 = (Student) ois.readObject(); Student s2 = (Student) ois.readObject(); //Student s3 = (Student) ois.readObject(); //當文件讀到末尾出現EOFException System.out.println(s1); System.out.println(s2); } }
序列化
package com.test.demo001; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.ArrayList; public class Demo010 { public static void main(String[] args) throws IOException{ Student s1 = new Student("z3",23); Student s2 = new Student("z4",24); Student s3 = new Student("z5",25); Student s4 = new Student("z6",26); ArrayList<Student> list = new ArrayList<>(); list.add(s1); list.add(s2); list.add(s3); list.add(s4); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.txt")); oos.writeObject(list); //將集合對象一次寫入 oos.close(); } }
反序列化
package com.test.demo001; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.util.ArrayList; public class Demo011 { public static void main(String[] args) throws IOException, ClassNotFoundException{ ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); ArrayList<Student> list = (ArrayList<Student>) ois.readObject(); //將集合對象一次讀取 for (Student stu : list) { System.out.println(stu); } ois.close(); } }
不先存檔(序列化)就讀檔(反序列化),會報錯。
private static final long serialVersionUID = 1L;