JAVA--高級基礎開發

Day12【緩衝流、轉換流、序列化流、打印流】

第一章  序列化流

    1. 概述
  1. Java 提供了一種對象序列化的機制,用一個字節序列能夠表示一個對象,該字節序列包含對象的數據、對象的類型、對象中存儲的屬性等信息。字節序列寫出文件以後,至關於文件中持久存儲了一個對象的信息。
  2. 反之,該字節序列還能夠從文件中讀取回來,重構對象,對它進行反序列化。對象的數據,對象的類型和對象中存儲的數據信息,均可以用來在內存中建立對象。

    1. ObjectOutputStream類
  1. java.io.ObjectOutputStream 類,將 Java 對象的原始數據類型寫進到文件,實現對象的持久存儲。
  2. 構造方法:
  • ObjectOutputStream(OutputStream out):建立一個寫入指定的 OutputStream 的

ObjectOutputStream。java

ObjectOutputStream fw= new數組

ObjectOutputStream( new FileOutputStream("F:\\aaa\\a.txt"));ide

 

  1. 序列化的操做
  2. 一個對象要想序列化,必須知足兩個條件:
  • 該類必須實現 java.io. Serializable 接口,Serializable 是一個標記接口,凡是實現該

接口的類的對象均可以被序列化。若是沒有實現此接口,那麼這個類的對象就不能學習

被序列化或者反序列化,不然會拋出 NotSerializableException。測試

  • 該類的全部屬性必須是可序列化的。若是有一個屬性不須要序列化,則該屬性必須

註明是瞬態的,使用 transient 關鍵字修飾,則這個屬性就不能被序列化。this

public class Student implements Serializable {編碼

    private static final long serialVersionUID = 727935469565319794L;idea

    private String name;spa

    private String address;.net

    private Integer  age;

    private Integer  score;

 

    public Student(){

 

    }

 

    public Student(String name, String address, Integer age, Integer score) {

        this.name = name;

        this.address = address;

        this.age = age;

        this.score = score;

    }

 

    public static long getSerialVersionUID() {

        return serialVersionUID;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getAddress() {

        return address;

    }

 

    public void setAddress(String address) {

        this.address = address;

    }

 

    public Integer getAge() {

        return age;

    }

 

    public void setAge(Integer age) {

        this.age = age;

    }

 

    public Integer getScore() {

        return score;

    }

 

    public void setScore(Integer score) {

        this.score = score;

    }

 

    @Override

    public String toString() {

        return "Student{" +

                "name='" + name + '\'' +

                ", address='" + address + '\'' +

                ", age=" + age +

                ", score=" + score +

                '}';

    }

}

 

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

 

//序列化父類java.io.OutputStream   子類:FileOutputStream

// 序列化:java.io.ObjectOutputStream繼承了OutputStream類

public class Meth04 {

    public static void main(String[] args) {

 

        //構造方法 ObjectOutputStream(OutputStream out)

        //建立一個寫入指定的OutputStream的ObjectOutputStream。

 

        //建立序列化對象

       //ObjectOutputStream fw=new

ObjectOutputStream(new FileOutputStream("F:\\aaa\\a.txt"));

 

        //建立序化的對象

        Student  student=new  Student("王智雅","山西市",20,99);

        Student  student1=new Student("李文傑","天水市",23,98);

        Student  student2=new Student("桑鳳嬌","泰安市",24,99);

        Student  student3=new Student("郭朝旭","聊城市",25,96);

        Student  student4=new Student("陳天意","承德市",27,100);

 

        try(ObjectOutputStream   fw=new

ObjectOutputStream(new FileOutputStream("F:\\aaa\\a.txt"));){

            fw.writeObject(student);

            fw.writeObject(student1);

            fw.writeObject(student2);

            fw.writeObject(student3);

            fw.writeObject(student4);

 

        }catch (FileNotFoundException ce ){

            ce.printStackTrace();

        }catch (IOException  ce){

            ce.printStackTrace();

        }

}

    1. ObjectInputStream類
  1. 位於java.io.ObjectInputStream 包中,反序列化流,將序列化的對象還原。
  2. 構造方法:

ObjectInputStream (InputStream  in):建立從指的InputStream 讀取的 ObjectInputStream。

  • 反序列化操: public final Object readObject():讀取一個對象
  • 示例以下所示:

//反序列化父類 java.io.InputStream   子類時FileinputStream

//java.io.ObjectInputStream 繼承了InputStream

 

//反序列化示例:

public class Meth05 {

public static void main(String[] args) {

 //建立反序列化的對象

        // ObjectInputStream  object=new

//ObjectInputStream(new FileInputStream("F:\\aaa\\a.txt"));

 

        //建立Student 對象

        Student  student=new Student();

        //讀文件

        try(ObjectInputStream  object=new

ObjectInputStream(new FileInputStream("F:\\aaa\\a.txt"));){

            Student ss=(Student)object.readObject();

             while(ss!=null){

                 System.out.println(ss);

                 ss=(Student)object.readObject();

             }

        }catch(ClassNotFoundException  ce){

            ce.printStackTrace();

        }catch (IOException  ce){

 

        }

}

}

/* 運行的結果:

Student{name='王智雅', address='山西市', age=20, score=99}

Student{name='李文傑', address='天水市', age=23, score=98}

Student{name='桑鳳嬌', address='泰安市', age=24, score=99}

Student{name='郭朝旭', address='聊城市', age=25, score=96}

Student{name='陳天意', address='承德市', age=27, score=100}*/

當 JVM 反序列化對象時,拋出了一個 InvalidClassException 異常,發生這個異常的緣由以下:

一、 該類的序列化版本號與從流中讀取的類描述的版本不匹配

二、  該類包含未知數據類型Serializable 接口給須要序列化的類,提供了一個序列化版本號,serialVersionUID 該版本號的目的在於驗證序列化的對象和對應類是否版本匹配。

第二章 緩衝流

上一章咱們學習了字節流和字符流,接下來咱們學習一些更強大的流,好比:可以高效讀寫

的緩衝流,可以轉換編碼的轉換流,可以持久化存儲對象的序列化流等待。這些功能更爲強

大的流,都是在基本流對象基礎之上建立而來的,這些功能更強大的流,至關因而對基本流

對象的一種加強。

2.1 概述

  1. 緩衝流: 也叫高效流,是對四個基本流的加強,按照數據格式劃分爲:
  2. 字節緩衝流和字符緩衝流。                                   
  3. 字節緩衝流:  BufferedInputStream、 BufferedOutputStream.
  4. 字符緩衝流:  BufferedReader、BufferedWriter.

緩衝流的基本原理:  是在建立流對象時,會建立一個內置的默認大小的緩衝區數組,經過緩衝區讀寫,(往內存中讀寫數據是最塊的),減小系統IO的次數,從而提升讀寫的效率。

2.2 BufferedInputStream字節緩衝流

2.2 BufferedOutputStream字節緩衝流

  1. 構造方法:
  • public BufferedInputStream(InputStream in) 建立一個新的緩衝流輸入對象。
  • public BufferedOutputStream(OutputStream out):建立一個新的緩衝流輸出對象
  1. 效率測試:示例

 

//普通的流實現數據的傳送:消耗的時間:16193毫秒

public class Meth06 {

    public static void main(String[] args) {

        //開始時間

        long  start=System.currentTimeMillis();

        try(FileInputStream  input=new FileInputStream("E:\\idea64.exe");

                FileOutputStream output=new FileOutputStream("F:\\copy.exe");){

            int i;

            while ((i=input.read())!=-1){

                output.write(i);

            }

 

        }catch (IOException  ce){

            ce.printStackTrace();

        }

        long  end=System.currentTimeMillis();

 

        System.out.println("複製普通文件的消耗的時間:"+(end-start)+"毫秒");

    }

}

  • 緩衝流實現:示例

 

// BufferedStream字節緩衝流實現傳送:消耗的時間:94毫秒

public class Meth07 {

    public static void main(String[] args) {

        //開始時間

        long  start=System.currentTimeMillis();

        try(BufferedInputStream  fr=new BufferedInputStream(new FileInputStream("E:\\idea64.exe"));

            BufferedOutputStream fw=new BufferedOutputStream(new FileOutputStream("F:\\copy1.exe"))){

            int i;

            while ((i=fr.read())!=-1){

                fw.write(i);

            }

 

        }catch (IOException  ce){

            ce.printStackTrace();

        }

        //結時間

        long  end=System.currentTimeMillis();

        System.out.println("複製普通文件的消耗的時間:"+(end-start)+"毫秒");

    }

}

  1. 更加高效的方式: 使用數組

// BufferedStream字節緩衝流用數組實現實現複製,更快

複製普通文件的消耗的時間:16毫秒

public class Meth08 {

    public static void main(String[] args) {

        //開始時間

        long  start=System.currentTimeMillis();

        try(BufferedInputStream  fr=new

BufferedInputStream(new FileInputStream("E:\\idea64.exe"));

            BufferedOutputStream fw=new

BufferedOutputStream(new FileOutputStream("F:\\copy2.exe"))){

            int i;

            //使用數組

            byte[]bytes=new byte[1024*8];

            while ((i=fr.read(bytes))!=-1){

                fw.write(bytes,0,i);

            }

 

        }catch (IOException  ce){

            ce.printStackTrace();

        }

        long  end=System.currentTimeMillis();

        System.out.println("複製普通文件的消耗的時間:"+(end-start)+"毫秒");

    }

}

2.3 字符緩衝流BufferedReader

2.4 字符緩衝流BufferedWriter

  1. 構造方法:
  • public  BufferedReader(Reader in) 建立使用默認大小的輸入緩衝區的緩衝字符輸入流
  • public  BufferedWriter(Writer out) 建立使用默認大小的輸出緩衝區的緩衝字符輸出流。
  • 經常使用的方法:
  • Public  String  readLine;每次讀取一行文字。
  • Public  int  read();每次讀取一個字符。
  • Public  String  newline(); 寫一行行分隔符。

1 字符緩衝流:BufferedReader 讀

 

//BufferedReader 字符緩衝流讀操做

public class Meth09 {

    public static void main(String[] args)throws  IOException{

 

        BufferedReader  fr=new BufferedReader(new FileReader("F:\\aaa\\a.txt"));

        String  s=null;

        //每次讀取一行的內容

        while ((s=fr.readLine())!=null){

            System.out.println(s);

        }

        fr.close();

    }

}

2 字符緩衝流: BufferedWriter 寫

//BufferedWriter 字符緩衝流 寫

public class Meth10 {

    public static void main(String[] args)throws  IOException{

        BufferedWriter  fw=new BufferedWriter(new FileWriter("F:\\aaa\\a.txt"));

        fw.write(65);

        //實現換行

        fw.newLine();

        fw.write("李文傑我愛個人祖國");

        fw.newLine();

        fw.write("hgsahgd");

        fw.newLine();

        fw.close();

    }

}

  • 轉換流

3.1 字符編碼和字符集

1計算機中儲存的信息都是用二進制表示的,而咱們在屏幕上看到的數字,英文,標點符號、

漢字等字符是二進制數轉換以後的結果。將字符存儲到計算機中,稱爲編碼。反之,將存儲在計算機中的二進制數按照某種規則解析出來,稱爲解碼。好比說,按照 A 規 則存儲,一樣按照 A 規則解析,那麼就能顯示正確的文本符號。反之,按照 A 規則存儲,再

按照 B 規則解析,就會致使亂碼現象。

2 字符編碼(Character Encoding)就是一套天然語言的字符與二進制數之間的對應規則。

3.2 字符集

  1. 字符集: Charset,也叫編碼表,是一個系統支持的全部字符的集合,包括各國的文字,圖形,標點符號,數字等。
  2. 計算機要準確的存儲和識別各類字符集數據,須要進行字符編碼,一套字符集必然有一套字符編碼,常見的字符編碼有ASCII字符集、GBK字符集、Unicode字符集。因此當指定了編碼,那麼他所對應的字符集也就指定了,因此編碼纔是咱們最終關心的。
  3. ASCII字符集:
  • ASCII字符集美國信息標準交換代碼。,是基於拉丁字母的電腦的一套編碼系統。用於顯示現代英語,主要包括控制字符(回車鍵、退格、換行鍵等)和可顯示字符(英文大小寫字符、阿拉伯數字和西文符號)
  • 基於ASCII碼字符集,使用7位(bites)表示一個字符,共128個字符。ASCII碼的擴展字符集使用8位,表示一個字符,共128 個字符,歐洲經常使用字符。
  • ISO-8859-1 字符集,拉丁碼錶,別名 Latin-1,用於顯示歐洲使用的語言,包括:荷蘭、丹麥、德語、意大利等。
  • ISO-8859-1 使用單字節編碼,兼容 ASCII 編碼.
  1. GBxxx:字符集。
  • GB 就是國標的意思,是爲了顯示中文而設計的一套字符集。
  • GB2312:簡體中文編碼表,一個小於 127 的字符的意義與原來相同;但兩個大於

127 的字符連在一塊兒就表示一個漢字,這樣大約能夠組合了包含 7000 多個簡體漢字。

  • GBK: 最經常使用的中文編碼表,是在 GB2312 的基礎上擴展規範,使用了雙字節編碼,

共收錄 20000 多個漢字,兼容 GB2312。

  • GB18030:  最新的中文編碼表。收錄漢子7萬多個,採用多字節編碼,每一個字節能夠由一個,兩個,四個組成。
  • Unicode字符集: Unicode 編碼系統爲爲表達任意語言的任意字符而設計的,是業界的一種標準,也稱爲統一編碼,標準萬國碼, 它最多使用 4 個字節的數字表達字母、符號、或者文字、有三種編碼方案,UTF-八、UTF-16 和 UTF-32.
  • UTF-8 編碼,能夠用來表示 Unicode 標準中任何字符,它是電子郵件、網頁及其餘存儲或傳輸文字應用中,優先採用的編碼。IETF 要求全部互聯網協議都必須支持UTF-8 編碼,因此咱們作程序開發,也要使用 UTF-8 編碼。編碼規則:
  •  128 個 US-ASCII 字符,只須要一個字節
  •  拉丁文字符,須要兩個字節
  •  中文,使用三個字節編碼

3.3 轉換流

  • InputStreamReader類,轉換流 java.io.InputStreamReader,是 Reader 的子類,是從字節流到字符流的橋樑,它讀取字節,並使用指定的字符集將其解碼爲字符。它的字符集能夠由名稱指定,也能夠接受平臺的默認字符集。
  • 構造方法:
  • InputStreamReader(InputStream in) 建立一個使用默認字符集的 InputStreamReader
  • InputStreamReader(InputStream in, String charsetName) 建立一個使用命名字符集的InputStreamReader。

//轉換流 InputStreamReader,將字節轉換爲字符,實現讀取的操做

public class Meth11 {

    public static void main(String[] args)throws  IOException{

        //指定字符的編UF碼

        InputStreamReader  input=new

InputStreamReader(new FileInputStream("F:\\aaa\\a.txt"),"UTF-8");

        int  i;

        while ((i=input.read())!=-1){

            System.out.print((char)i);

        }

        input.close();

    }

}

  • OutputStreamWriter類: 轉換流 java.io.OutputStreamWriter, Writer 的子類,是從字符流到字節流的橋樑,使用指定的字符集將字符編碼爲字節,它的字符集能夠由名稱指定,也能夠接受平臺默認的字符集.
  • 構造方法:
  • OutputStreamWriter(OutputStream out) 建立一個使用默認字符OutputStreamWriter。
  • OutputStreamWriter(OutputStream out, String charsetName) 建立一個使用命名字符集的OutputStreamWriter。

 

//轉換流 OutputStreamWriter 將字符轉換爲字節,實現存儲的操做(寫

public class Meth12 {

    public static void main(String[] args)throws  IOException{

 //指定字符的編碼

        OutputStreamWriter  fr=new

OutputStreamWriter(new FileOutputStream("F:\\aaa\\a.txt"),"UTF-8");

        fr.write("李文傑@1314520");

        fr.write("sangfenjirjie");

        fr.write("liwnejie");

         fr.close();

    }

}

第四章  打印流

4.1 概述

  1. 咱們在控制檯打印輸出內容,是調用的 print 方法或者 println 方法完成的,這兩個方法搜來自於 java.io.PrintStream 類,該類可以方便地打印各類數據類型的信息,是一種便捷的輸出流。
  2. PrintStream類

 

//打印流,只有輸出流,沒有輸入流

public class Meth13 {

    public static void main(String[] args)throws IOException{

        //static void setOut(PrintStream out);打印流

        PrintStream  print=new PrintStream("F:\\aaa\\a.txt");

        System.setOut(print);

        System.out.println("liwnejie");

        System.out.println("sangfengjiao");

        System.out.println("wangzhiys");

    }

}

相關文章
相關標籤/搜索