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

    }

}

相关文章
相关标签/搜索