Java学习day11--IO流总结

IO流总结

以下是总结的IO流图:
在这里插入图片描述

输出流OutputStream(抽象类)

常用其基本子类FileOutputStream构造,OutputStream的常用方法有:

  1. write​(int b) 将指定的字节写入此输出流。b为某字符的byte值。
  2. write​(byte[] b) 将 b.length字节从指定的字节数组写入此输出流。
  3. write​(byte[] b, int off, int len) 从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。
try (OutputStream out1 = new FileOutputStream("c.txt")) {
            // 1. 直接写入字节
           // out1.write('g');
            out1.write('c');
            // 2. 写入字节数组
            byte[] b = "爱老虎油".getBytes();
            out1.write(b);
            // 3. 写入数组的一部分
            byte[] b2 = {97, 98, 99, 100};
            out1.write(b2, 0, 2);   // 从0的位置开始向后写入数组b2的两个元素
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
}

FileOutputStream类

该部分主要讲解构造方法;

  1. FileOutputStream​(File file) 创建文件输出流以写入由指定的 File对象表示的文件。如果文件存在且有内容会覆盖。
  2. FileOutputStream​(File file, boolean append) 创建文件输出流以写入由指定的 File对象表示的文件。追加 。
  3. FileOutputStream​(String name) 创建文件输出流以指定的名称写入文件。 覆盖。
  4. FileOutputStream​(String name, boolean append) 创建文件输出流以指定的名称写入文件。 追加。

BufferedOutputStream缓冲流

利用BufferedOutputStream类会在电脑硬盘与程序之间建立缓冲区,默认是8k,用于将读取的内容暂存在内存中,从而加快程序读取内容的效率。
构造方法有:

  1. BufferedOutputStream​(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
  2. BufferedOutputStream​(OutputStream out, int size) 创建一个新的缓冲输出流,以便以指定的缓冲区大小将数据写入指定的底层输出流。 size是缓冲区的大小,默认8k。
public class Demo {
    public static void main(String[] args) {
        try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("a.txt", true));    // 由于OutputStream是抽象类,故用FileOutputStream来创建。
        BufferedInputStream in = new BufferedInputStream(new FileInputStream("配置文件.txt"))) {    // 输出流--缓冲流
            int t = 0;
            byte[] b = new byte[1024];
            while ((t = in.read(b)) != -1) {
                out.write(b, 0, t);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注:其写入方法write()用法相同。

ObjectOutputStream(序列化流)

1).什么是“序列化”:将一个“对象”连同“属性值”整体存储到一个文件中,这个过程叫:序列化。
2).什么是“反序列化”:将之前被序列化的对象,再读取到内存中,并重新生成对象,并设置属性值,这个过程叫:反序列化。

注意:被序列化的类,必须实现java.io.Serializable接口

  1. 构造方法
    ObjectOutputStream​(OutputStream out) 创建一个写入指定的OutputStream的ObjectOutputStream。
  2. writeObject(Object obj); 序列化的写入方法
public class Demo {
    public static void main(String[] args) {
        Student stu = new Student("成龙", 17);

        //序列化
        try (ObjectOutputStream oos = new ObjectOutputStream(
						new FileOutputStream("demo07_obj.txt"))) {

            //序列化一个对象
            oos.writeObject(stu);//将Student对象连同属性值一起存储到了demo07_obj.txt文件中

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注:字节流的输入方法与输出方法用法几乎相同,这里就不再列举了。

字符输出流Writer(抽象类)

1.write​(int c) 写一个字符
2.void write​(String str) 写一个字符串
3.void write​(String str, int off, int len) 写一个字符串的一部分。

FileWriter(基本流)

  1. FileWriter​(File file) 给一个File对象构造一个FileWriter对象。
  2. FileWriter​(File file, boolean append) 给一个File对象构造一个FileWriter对象。
  3. FileWriter​(String fileName) 构造一个给定文件名的FileWriter对象。
  4. FileWriter​(String fileName, boolean append) 构造一个FileWriter对象,给出一个带有布尔值的文件名,表示是否附加写入的数据。

BufferedWriter(缓冲流)

  1. BufferedWriter​(Writer out) 创建使用默认大小的输出缓冲区的缓冲字符输出流。
  2. BufferedWriter​(Writer out, int sz) 创建一个新的缓冲字符输出流,使用给定大小的输出缓冲区。
    注:这里的Writer类型常用 FileWriter来构造。

OutputStreamWriter(转换流)

转换流是字符与字节之间的桥梁,例如读取GBK编码的文件时,普通读法是直接读取,由于程序默认是以utf8编码都进来的,故而出现乱码。此时我们就要指定编码格式。

  1. OutputStreamWriter​(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
  2. OutputStreamWriter​(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
public class Demo {
    public static void main(String[] args) {
        try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt"), "GBK");
             InputStreamReader in = new InputStreamReader(new FileInputStream("测试文件.txt"))) {
            int t = 0;
            char[] ch = new char[1024];
            while ((t = in.read(ch)) != -1) {
                out.write(ch, 0, t);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

同样,转换流的输入流用法相同,这里就不再列举了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值