FileOutputStream字节输出流, FileInputStream字节输入流, 字节缓冲流, FileReader字符输入流, FileWriter字符输出流

IO流

  • I : input 输入 (读取)
  • O : output 输出 (写出)

作用 : 数据传输
image.png

IO流体系结构

image.png

FileOutputStream字节输出流

image.png

注意事项

  • 关联的文件如果不存在:自动创建
  • 存在:会清空现有的,再开始写入

image.png

细节

  1. 文件如果不存在, 会自动创建
  2. 文件如果存在, 会先清空, 再写入
  • 问题: 想要追加写入, 怎么办?
    • 回答: 在构造方法的第二个参数传入true, 开启追加写入模式
  • 问题: 咋换行啊?
    • 回答: 写出数据的时候, 加入"\r\n"
/*
    字节输出流写出数据

    细节:
            1. 文件如果不存在, 会自动创建
            2. 文件如果存在, 会先清空, 再写入
                    问题: 想要追加写入, 怎么办?
                    回答: 在构造方法的第二个参数传入true, 开启追加写入模式
                    问题: 咋换行啊?
                    回答: 写出数据的时候, 加入"\r\n"
 */
public static void main(String[] args) throws IOException {
    // 1. 创建字节输出流对象关联文件
    FileOutputStream fos = new FileOutputStream("day10-code\\A.txt", true);
    // 2. 写出数据
    fos.write("你好, 大家好, 真好!\r\n".getBytes());
}

流对象使用完毕后, 记得调用close方法关闭, 不然会占用资源

FileOutputStream fos = null;

try {
    fos = new FileOutputStream("day10-code\\B.txt");
    fos.write(97);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if(fos != null){
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

finally代码块中包裹的代码,是一定会执行的代码(抬杠不算)

/*
    jdk7版本开始的IO标准异常处理代码.

    try
        (需要关流的对象)
        {流对象的业务代码}
    catch(异常类名 对象名){
        异常处理方案.
    }

    总结: 这种格式编写代码, try()中的对象, 会自动的调用close方法.

    原理: 需要关流的对象, 实现了AutoCloseable接口.
 */
public static void main(String[] args) {

    try (FileOutputStream fos = new FileOutputStream("day10-code\\B.txt");) {
        fos.write(97);
    } catch (IOException e) {
        e.printStackTrace();
    }


}
}

class A implements AutoCloseable {

@Override
public void close() throws NullPointerException {
    System.out.println("我关了...");
}

标准的关流代码

image.png

注意事项

try () 中的对象, 需要实现过 AutoCloseable接口

总结
  • 这种格式编写代码,try()中的对象,会自动的调用close方法。
原理
  • 需要关流的对象,实现了AutoCloseable接口。

总结

  • 创建对象的时候和文件建立关联
  • 通过write方法写出数据
    1. 写出单个字节
    2. 写出一个字节数组
    3. 写出字节数组的一部分
  • 最后记得关流释放资源

FileInputStream字节输入流

image.png

注意事项

关联的文件不存在会抛出FileNotFoundException异常 文件夹的话会拒绝访问
image.png

/*
    FileInputStream字节输入流读取文件
    
    public int read() : 一次读取一个字节, 读取到文件末尾, 返回-1标记
    public int read(byte[] bys) : 一次读取多个字节, 存入数组, 并返回读取到的有效字节个数.
 */
public static void main(String[] args) throws IOException {

    FileInputStream fis = new FileInputStream("day10-code\\B.txt");

    byte[] bys = new byte[2];

    int len;
    while ((len = fis.read(bys)) != -1) {
        String s = new String(bys, 0, len);
        System.out.print(s);
    }

    fis.close();

}

private static void method() throws IOException {
    FileInputStream fis = new FileInputStream("day10-code\\B.txt");

    int i;

    while ((i = fis.read()) != -1) {
        System.out.print((char) i);
    }

    fis.close();
}

image.png

字节缓冲流

  • 字节缓冲流在源代码中内置了字节数组,可以提高读写效率

image.png
image.png

注意

  • 缓冲流不具备读写功能,它们只是对普通的流对象进行包装

真正和文件建立关联的,还是普通的流对象

/*
    缓冲流的作用: 内置了字节数组, 提高读写效率
 */
public static void main(String[] args) throws IOException {
    // 1. 创建字节缓冲输入流
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("day10-code\\B.txt"));

    // 2. 创建字节缓冲输出流
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day10-code\\C.txt"));

    // 3. 拷贝
    int i;
    while((i = bis.read()) != -1){
        bos.write(i);
    }

    bis.close();
    bos.close();
}

image.png

public static void main(String[] args) throws IOException {
    // 创建字节缓冲输出流, 内置8192大小的数组
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\A.txt"));

    for(int i = 1; i <= 8193; i++){
        bos.write(97);
    }

}

private static void input() throws IOException {
    // 创建字节缓冲输入流, 内置8192大小的数组
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("day10-code\\B.txt"));

    int i = bis.read();
    System.out.println(i);

    bis.close();
}

FileReader字符输入流

  • 用于读取纯文本文件,解决中文乱码问题

image.png

字符

  • 字符集: 是指多个字符的集合
  • 字符编码:字符编码是指一种映射规则
    • 英文字符:占用一个字节,使用正数表示
    • 中文字符:
      • GBK占用2个字节
      • Unicode(UTF-8)占用3个字节
      • 中文字节以负数开头

编码和解码

  • 编码: 字符转字节

image.png

  • 解码: 字节转字符

image.png

// 编码: 将能看懂的字符, 转换为看不懂的字节.
String s = "你好,你好";
byte[] utf8_bytes = s.getBytes();
System.out.println(Arrays.toString(utf8_bytes));

byte[] gbks = s.getBytes("gbk");
System.out.println(Arrays.toString(gbks));

// 解码: 将看不懂的字节, 转换为看得懂的字符
String s1 = new String(utf8_bytes);
System.out.println(s1);

String s2 = new String(gbks, "GBK");
System.out.println(s2);

FileWriter字符输出流

image.png

注意事项

  • 字符输出流写出数据,需要调用Flushclose方法,数据才会写出
    • Flush后可以继续写出
    • Close后不能写出
/*
    注意:
            1. 字节流写出数据如果没有关流, 数据能够写出
            2. 字符流写出数据如果没有关流(close), 或者是刷出(flush), 数据不会到达文件.

                        close() 操作之后, 不能继续写出数据
                        flush() 操作之后, 可以继续写出数据

            细节: close() 操作字符流的时候, 不但可以将流对象关闭, 还可以将缓冲区的数据, 刷出到文件.

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

    FileWriter fw = new FileWriter("day10-code\\D.txt");

    char[] chs = {'a','b','c','d','e'};

    fw.write('你');
    fw.write(chs);
    fw.write(chs,2,2);
    fw.write("哈哈哈哈哈哈");
    fw.write("大家好", 0, 2);

    fw.close();

}
  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值