【Java八股文之基础篇(八)】IO流的面试高频问题

IO流

编码表

计算机要准确的存储和识别各种字符集符号,就需要进行字符编码,一套字符集必然至少有一套字符编码。 如果编码和解码不是用一个编码表就会出现乱码问题。

编码(加密):把看懂的–>看不懂(二进制)
解码(解密):把看不懂(二进制)–>看懂的

常见的编码表

  • ASCII
    ASCII的扩展字符集使用8位表示一个字符,共256字符【控制字符(回车键、退格、换行键等)和可显示字符(英文大小写字符、阿拉伯数字和西文符号)】。方便支持欧洲常用字符。是一个系统支持的所有字符的集合。

  • GBK
    最常用的中文码表。是在GB2312标准基础上的扩展规范,使用了双字节编码方案,共收录了21003个汉字,完全兼容GB2312标准,同时支持繁体汉字以及日韩汉字等

  • Unicode
    UTF-8编码:可以用来表示Unicode标准中任意字符,它是电子邮件、网页及其他存储或传送文字的应用中,优先采用的编码。互联网工程工作小组(IETF)要求所有互联网协议都必须支持UTF-8编码。它使用一至四个字节为每个字符编码

128个US-ASCII字符,只需一个字节编码
拉丁文等字符,需要二个字节编码
大部分常用字(含中文),使用三个字节编码
其他极少使用的Unicode辅助字符,使用四字节编码

  • ANSI
    其实不是具体的编码表,它指代系统的默认编码表。例如:简体中文的Windows系统默认编码是GBK。

字符流

当需要读取/写入的数据是纯文本的形式时我们可以使用字符流来进行操作会更加方便。与此相反的是图片音频这种多媒体形式,字节流方便点。

字符输入流

所有字符输入流的父类是 java.io.Reader ,它是以字符为单位的输入流 。
我们就以其子类FileReader为例进行学习。

FileReader概述

FileReader 是用来从文件中读取数据的字符输入流。

FileReader创建对象

构造方法如下

public FileReader(String fileName) throws FileNotFoundException //传入文件路径创建对象
public FileReader(File file) throws FileNotFoundException //传入文件路径的File对象来创建流对象 

范例

public static void main(String[] args) throws FileNotFoundException {

        FileReader fr = new FileReader("C:\\Users\\root\\Desktop\\test\\汉字2.txt");

        FileReader fr2 = new FileReader(new File("C:\\Users\\root\\Desktop\\test\\汉字2.txt"));
    }
读取数据
一次读取一个字符

核心方法:

public int read() throws IOException //一次读取一个字符返回,如果读到文件末尾,返回值为-1

范例

public static void main(String[] args) throws IOException {
        //创建流对象
        FileReader fr = new FileReader(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));

        //调用方法
        int ch;
        while((ch=fr.read())!=-1){
            System.out.println((char)ch);
        }
		//假设11.txt是使用UTF-8编码,写入的是“章ab”,其中“章”占3个字节,字母占1个字节。
		//字符流读取该文件,按道理它返回的原始数据是int值,比如章返回22580、a返回97、b返回98.我们强转char类型,可以得到“章ab”。
        //释放资源
        fr.close();
    }
一次读取一个字符数组

核心方法:

public int read(char cbuf[]) throws IOException //一次读取一个字符数组 返回值为-1时代表读到了末尾

范例

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

        //创建流对象
        FileReader fr = new FileReader(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));

        //读取
        char[] chars = new char[1024];
        int len;
        while((len=fr.read(chars))!=-1){
            //说明读到了内容
            System.out.println(chars);
        }
        //释放资源
        fr.close();
    }

字符输出流

所有字符输出流的父类是  java.io.Writer ,它是以字符为单位的输出流 。
我们就以FileWriter为例进行学习。
FileWriter概述

FileWriter是用来往文件写入数据的字符输出流。

FileWriter创建

构造方法:

public FileWriter(String fileName) throws IOException //传入文件路径创建对象
public FileWriter(File file) throws IOException      //传入文件路径的File对象来创建流对象

范例

 public static void main(String[] args) throws IOException {
        
        FileWriter fw = new FileWriter("C:\\Users\\root\\Desktop\\test\\11.txt");

        FileWriter fw2 = new FileWriter(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));
    }
写入数据
一次写一个字符

核心方法

public void write(int c) throws IOException //把一个字符写入目的地
public void flush() throws IOException //把缓存区中的数据写入硬盘

示例:

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

        //创建流对象
        FileWriter fw = new FileWriter(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));

        //写数据
       fileWriter.write(97);
        fileWriter.write(98);
        fileWriter.write('八');
        fileWriter.write('K');//先把K转为int,再写入
        fileWriter.write(147456);//底层原理就是utf-8编码表里有一个字符’䀀‘,它对应的二进制再转换为十进制是147456
        //释放资源
        fw.close();
    }

写入的结果:ab八K䀀

一次写一个字符数组

核心方法如下:

public void write(char cbuf[]) throws IOException //把一个字符数组写入目的地

范例:

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

        //创建流对象
        FileWriter fw = new FileWriter(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));

        //写数据
        char[] chars = "一二".toCharArray();
        fw.write(chars);

        fw.flush();

        chars = "三四".toCharArray();
        fw.write(chars);

        //释放资源
        fw.close();
    }

写的结果就是:一二三四

一次写一个字符串

核心方法:

public void write(String str) throws IOException //把一个字符串写入目的地

范例:

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

        //创建流对象
        FileWriter fw = new FileWriter(new File("C:\\Users\\root\\Desktop\\test\\11.txt"));

        fw.write("一二三四");
        fw.flush();

        //释放资源
        fw.close();
    }
如何选择

随机应变,有什么类型的数据就使用对应的重载。

练习

使用字符流实现纯文本文件的复制

public static void main(String[] args) throws IOException {
        //1.创建流对象
        File file = new File("C:\\Users\\root\\Desktop\\test\\11.txt");
        FileReader fr = new FileReader(file);
        FileWriter fw = new FileWriter("C:\\Users\\root\\Desktop\\test\\22.txt");

        //2.循环读写
        char[] chars = new char[3];
        int len;
        while((len=fr.read(chars))!=-1){
            //把读到的内容写入新文件中
            fw.write(chars,0,len);
            //fw.flush();
        }

        //3.释放资源
        fw.close();
        fr.close();
    }

未完待续。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kplusone

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值