Java学习Day18

2019-09-27  本文已影响0人  JayMeWangGL

今日学习内容总结


缓冲流

缓冲流,也叫高效流,是对4个基本的FileXxx 流的增强,所以也是4个流,按照数据类型分类:

缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率。

字节缓冲流

构造方法

BufferedOutputStream使用步骤

  1. 创建FileOutputStram对象,构造方法中要绑定要输出的目的地
  2. 创建BufferedOutputStream对象,构造方法中传输FileOutputStream对象,提高FileOutputStream对象效率
  3. 使用BufferedOutputStream对象中的方法write,把数据写入内部缓冲区中
  4. 使用BufferedOutputStream对象中的方法flush,把内部缓冲区的数据刷新到文件中
  5. 释放资源(会自动调用flush方法)
public static void main(String[] args) throws IOException {
        FileOutputStream fos =new FileOutputStream("d:\\z.txt",true);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write(97);
        bos.close();
    }

BufferedInputStream使用步骤

  1. 创建FileInputStream对象,构造方法中要绑定要读取的文件
  2. 创建BufferedInputStream对象,构造方法中传输FileInputStream对象,提高FileInputStream对象的读取效率
  3. 使用BufferedInputStream对象中的方法read,读取文件
  4. 释放资源(会自动调用flush方法)
public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("d:\\z.txt");
        BufferedInputStream bis =new BufferedInputStream(fis);
        int read=0;
        while((read=bis.read())!=-1){
            System.out.println(read);
        }
        bis.close();
    }

字符缓冲流

构造方法

特有方法

字符缓冲流的基本方法与普通字符流调用方式一致,不再阐述,我们来看它们具备的特有方法。

使用步骤与字节流类似

写入数据:

public static void main(String[] args) throws IOException {
        BufferedWriter Bw = new BufferedWriter(new FileWriter("d:\\y.txt"));
        for (int i = 0; i <10 ; i++) {
            Bw.write("TestBufferedWriter");
            Bw.newLine();
        }
        Bw.close();
    }

读取数据:

 public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:\\y.txt"));
        String line;
        while ((line=br.readLine())!= null){
            System.out.println(line);
        }
        br.close();
    }

转换流

字符编码和字符集

字符编码

编码:字符(能看懂的)--字节(看不懂的)

解码:字节(看不懂的)-->字符(能看懂的)

字符集

OutputStreamWriter类

构造方法

使用步骤:

  1. 创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
  2. 使用OutputStreamWriter对象中的方法write,把字符转换为字节存储在缓冲区
  3. 使用OutputStreamWriter对象中的方法flush,把内部缓冲区的数据刷新到文件中
  4. 释放资源(会自动调用flush方法)
public static void main(String[] args) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\GBK.txt"),"GBK");
        osw.write("你好");
        osw.close();
    }

InputStreamReader类

构造方法

使用步骤:

  1. 创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
  2. 使用InputStreamReader对象中的方法read读取文件
  3. 释放资源(会自动调用flush方法)
    注意:构造方法中指定的编码表名称要跟文件的编码相同,否则会发生乱码
public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\GBK.txt"),"GBK");
        int len=0;
        while((len=isr.read())!=-1){
            System.out.println((char)len);
        }
        isr.close();
    }

将GBK格式转换为UTF-8格式:

public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\GBK.txt"),"GBK");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\GBK_u8.txt"),"UTF-8");
        int len=0;
        while((len=isr.read())!=-1){
            osw.write(len);
        }
        isr.close();
        osw.close();
    }

序列化

3.1 概述

Java 提供了一种对象序列化的机制。用一个字节序列可以表示一个对象,该字节序列包含该对象的数据对象的类型对象中存储的属性等信息。字节序列写出到文件之后,相当于文件中持久保存了一个对象的信息。

反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化对象的数据对象的类型对象中存储的数据信息,都可以用来在内存中创建对象。

3.2 ObjectOutputStream类

ObjectOutputStream类,将Java对象的原始数据类型写出到文件,实现对象的持久存储。

构造方法

特有方法:

序列化操作

  1. 一个对象要想序列化,必须满足两个条件:
public class Person implements Serializable {
    private String name;
    private int age;
    ...
    }

public static void main(String[] args) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\person.txt"));
        oos.writeObject(new Person("zkn",22));
        oos.close();
    }

ObjectInputStream类

ObjectInputStream反序列化流,将之前使用ObjectOutputStream序列化的原始数据恢复为对象。

构造方法

反序列化操作前提

如果能找到一个对象的class文件,我们可以进行反序列化操作,调用ObjectInputStream读取对象的方法:

public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\person.txt"));
        Object o = ois.readObject();
        ois.close();
        System.out.println(o);
    }

打印流

java.io.PrintStream类,该类能够方便地打印各种数据类型的值,是一种便捷的输出方式。

PrintStream类

构造方法

public class PrintDemo {
    public static void main(String[] args) throws IOException {
        // 调用系统的打印流,控制台直接输出97
        System.out.println(97);
      
        // 创建打印流,指定文件的名称
        PrintStream ps = new PrintStream("ps.txt");
        
        // 设置系统的打印流流向,输出到ps.txt
        System.setOut(ps);
        // 调用系统的打印流,ps.txt中输出97
        System.out.println(97);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读