IO流~~

ASCII

ASCII使用一个字节存储一个字符,一个字节是8位,共可表示128个字符信息。
在这里插入图片描述

GBK

window系统默认的码表。兼容ASCII码表,也包含了几万个汉字,并支持繁体汉字以及部分日韩文字
GBK是中国的码表,一个中文以两个字节的形式存储,但里面的英文还是一个字节

Unicode

在这里插入图片描述
在这里插入图片描述

字符集的编码和解码操作

在这里插入图片描述

IO流

IO流也称为输入、输出流,就是用来读写数据的
I表示input,是数据从硬盘文件读入到内存的过程,称为输入,负责读
O表示output,是内存程序 的数据从内存到写出硬盘文件的过程,称之输出,负责写
在这里插入图片描述

IO流的分类

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

使用的话,肯定是使用抽象类下面的实现类(子类)

字节流的使用

FileInputStream

在这里插入图片描述

由于read()方法读取中文时,一次也是一个字节(UTF-8编码中文占3个字节),会出现乱码

准备一个txt文件,内容如下
在这里插入图片描述

每次读取一个字节
public class Test {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream fileInputStream = new FileInputStream("test.txt");
        int i = fileInputStream.read();
        while (i!=-1){
            System.out.print((char)i);
            i = fileInputStream.read();
        }
    }
}

显然遇到中文字符很出现乱码
在这里插入图片描述

每次读取一个字节数组
public class Test {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream fileInputStream = new FileInputStream("test.txt");
        byte[] bytes = new byte[3]; //每次读三个字节
        int i = fileInputStream.read(bytes);
        while (i!=-1){
            System.out.print((new String(bytes)));
            i = fileInputStream.read(bytes);
        }
    }
}

存在乱码和错误的可能
在这里插入图片描述

一次读完全部字节

两种方法:1.让byte数组直接等于文件大小;2.使用readAll()方法
在这里插入图片描述

public class Test {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream fileInputStream = new FileInputStream("test.txt");
        File file = new File("test.txt");
        byte[] bytes = new byte[(int)file.length()]; //每次读三个字节
        int i = fileInputStream.read(bytes);
        System.out.println(new String(bytes));
    }
}
public class Test {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream fileInputStream = new FileInputStream("test.txt");
//        File file = new File("test.txt");
//        byte[] bytes = new byte[(int)file.length()]; //每次读三个字节
//        int i = fileInputStream.read(bytes);
        System.out.println(new String(fileInputStream.readAllBytes()));
    }
}

这两种一次读完全部字节的方式,都不会出现乱码

写字节数据到文件

文件字节输出流(FileOutputStream)写数据出去的API
写数据必须要刷新流,只有这样才能让写出去的数据生效
当FileOutputStream管道一旦创建就会清空原来文件种的数据,若要追加数据要用不同的构造方法
若要换行,直接写入"\r\n"
在这里插入图片描述
在这里插入图片描述

文件拷贝

思路:1. 根据数据源创建字节输入流对象 2.根据目的地创建字节输出流对象 3.读写数据,复制文件
任何文件都是有字节组成的,拷贝是一字不漏的转移字节,只要前后编码格式一致就行

public class Test {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream fileInputStream = new FileInputStream("test1.mp4");
        byte[] mp4Bytes = fileInputStream.readAllBytes();
        FileOutputStream fileOutputStream = new FileOutputStream("testout.mp4");
        fileOutputStream.write(mp4Bytes);
        fileInputStream.close();
        fileOutputStream.close();
    }
}
资源释放的方式

try-catch-finally
try-with-resource
也就是在捕获异常之后释放资源

try-catch-finally

因为finally是否出现异常都会被执行
在释放资源之前要进行非空校验,防止空指针异常
注意:不建议在finally里面加return,一旦加了return,那返回的永远是finally里面的数据
在这里插入图片描述

try-catch-resource

释放资源的改进做法
在这里插入图片描述

在这里插入图片描述

字符流的使用

读取中文使用字符流更合适

在这里插入图片描述

文件字符输入流:Reader

在这里插入图片描述

每次读一个字符

public class Test {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Reader reader = new FileReader("test.txt");
        int code;
        while ((code = reader.read())!=-1){
            System.out.print((char)code);
        }
    }
}

每次读一个字符数组

public class Test {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Reader reader = new FileReader("test.txt");
        char[] chars = new char[3];
        int code;
        while ((code = reader.read(chars))!=-1){
            System.out.print(new String(chars));
        }
    }
}
文件字符输出流:Writer

注意写入之后要进行刷新或关闭
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值