【210天】黑馬程序員27天視頻學習筆記【Day22-上】

叨逼叨兩句

  1. 快樂來的越容易,痛苦也就會來的越頻繁。java

  2. 若是沒有互聯網,也許你這輩子真就受困於你身邊圈子的認知了。面試

22-(01-02):序列流(整合兩個流對象+整合多個流對象)

  1. 序列流的存在價值是:幫助整合多個文件的內容放到一個文件中。數組

  2. 整合多個時,須要使用枚舉的方式。優化

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();
    }
}

22-03:內存輸出流

概述

  1. 該輸出流能夠向內存中寫入數據,它是把內存做爲一個緩衝區,寫出以後一次性將數據取出,經常使用於聊天應用。編碼

  2. 數組和集合在內存中用完就自動釋放了,不像流,跟硬盤有關聯,須要手動關閉。code

使用方式

  1. 建立對象:new ByteArrayOutputStream()對象

  2. 寫出數據:write(int),write(byte[])接口

  3. 獲取數據: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);
        
    }

22-04:內存輸出流之黑馬面試題

  1. FileInputStream讀取中文時出現亂碼的解決方案:內存

    1. 字符流

    2. 內存輸出流: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();
    }
}

22-(05-08)

對象操做流ObjectOutputStream(序列化、存檔)

  1. 序列化:將對象寫到文件上【相似遊戲存檔】

  2. 反序列化:將對象從文件中讀取出來【相似遊戲讀檔】

  3. 對象寫到文件上會是亂碼,但木有關係,能讀出來就行。

  4. 對象必須是可序列化的,即它必須實現Serializeable接口

  5. Serializeable接口中沒有任何方法,它就是一個標記而已

  6. 若本身不指定,則類的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();
        
    }
}

對象操做流ObjectInputStream(反序列化、讀檔)

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);
    }
}

對象操做流優化

  1. 序列化

    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();
        }
    }
  2. 反序列化

    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();
            
        }
    }

ID號(版本號)

  1. 不先存檔(序列化)就讀檔(反序列化),會報錯。

  2. private static final long serialVersionUID = 1L;

相關文章
相關標籤/搜索