重学java 64.IO流 字符流

Action speak louder than words

------ 24.6.5

字符输入流

一、字节流读取中文的问题

1.注意:

字节流是万能流,这个万能更侧重于文件复制,但是尽量不要边读边看

2.原因:

UTF-8:一个汉字占三个字节

**GBK:**一个汉字占两个字节

如果按照字节读取,每次读取的字节没有构成一个汉字的字节数,就直接输出,汉字是显示不了的

3.解决:

将文本文档中的内容,按照字符去操作

注意:

① 按照字符去操作编码也要一致,如果不一致,会乱码

② 按照字节流去操作,即使编码一致,边读边看也有可能会乱码

二、FileReader的介绍以及使用

字符流专门操作文本文档的,但是复制操作不要用字符流,要用字节流

1.概述

字符输入流 ---> Reader ---> 是一个抽象类

子类:FileReader

2.作用

将文本文档中的内容读取到内存中来

3.构造

FileReader(File file)

FileReader(String path)

4.方法

int read()---> 一次读取一个字符,返回的是读取字符对应的int值

int read(char[] cbuf) ---> 一次读取一个字符数组,返回的是读取个数

int read(char[] cbuf,int off,int len) ---> 一次读取一个字符数组一部分,返回的是读取个数

chuf:读取的数组

off:读取开始的位置

len:读取的长度

void close()---> 关闭资源

java 复制代码
import java.io.FileReader;
import java.io.IOException;

public class Demo270FileMethod {
    public static void main(String[] args) throws IOException {
        method01();
        method02();

        // int read(char[] cbuf) ---> 一次读取一个字符数组,返回的是读取个数
    }

    private static void method02() throws IOException {
        FileReader fr = new FileReader("AllWillBest_Java/1.txt");
        char[] buf = new char[3];
        int len;
        while ((len = fr.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }
    }

    private static void method01() throws IOException {
        FileReader fr = new FileReader("AllWillBest_Java/1.txt");
        // int read() ---> 一次读取一个字符,返回的是读取字符对应的int
        int len;
        while ((len = fr.read()) != -1) {
            System.out.print((char)len);
        }
        fr.close();
    }
}

三、FileWriter的介绍以及使用

1.概述:

字符输出流 ---> writer ---> 抽象类

子类:Filewriter

2.作用:

将数据写到文件中

3.构造:

Filewriter(File file)

Filewriter(string fileName)

FileWriter(string fileName,boolean append)****->追加,续写

4.方法

void write(int c)---> 一次写一个字符

void write(char[] cbuf)---> 一次写一个字符数组

void write(char[] cbuf, int off, int len)---> 一次写一个字符数组的一部分

void write(string str)---> 直接写一个字符串

void close() --->关流

5.FileWriter的刷新功能和关闭功能

void flush() --->将缓冲区的文件送到文件内,后续流对象才能继续使用(只刷新)

void close() --->先刷新后关闭,后续流对象不能使用了(刷新且关闭)

java 复制代码
import java.io.FileWriter;
import java.io.IOException;

public class Demo271FileWriter {
    public static void main(String[] args) throws IOException {
        method01();
    }

    private static void method01() throws IOException {
        FileWriter fw = new FileWriter("AllWillBest_Java/1.txt");
        fw.write("我一直相信,");
        fw.write("苦难是花开的伏笔,");
        fw.write("流水不争先,争的是滔滔不绝");
        fw.flush();
        fw.close();
    }
}

四、IO流异常处理问题

1.格式

try{

}catch{

}

2.示例

java 复制代码
import java.io.FileWriter;
import java.io.IOException;

public class Demo272Exception {
    public static void main(String[] args) {
        FileWriter fw = null;
        try{
            fw = new FileWriter("AllWillBest_Java\\2.txt");
            fw.write("一切都会好的");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 如果fw不为null,证明new出来了,需要ckose
            if(fw!=null){
                try {
                    fw.close();
                }catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

五、JDK7之后io异常处理方式

1.格式:

try(IO对象){
可能出现异常的代码

}catch(异常类型 对象名){

处理异常

}

2.注意:

以上格式处理IO异常,会自动关流

3.示例

java 复制代码
import java.io.FileWriter;

public class Demo273Exception2 {
    public static void main(String[] args) {
        try(FileWriter fw = new FileWriter("AllWillBest_Java\\3.txt");){
            fw.write("我一直相信");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
相关推荐
碳苯1 分钟前
【rCore OS 开源操作系统】Rust 枚举与模式匹配
开发语言·人工智能·后端·rust·操作系统·os
wclass-zhengge13 分钟前
数据结构篇(绪论)
java·数据结构·算法
何事驚慌14 分钟前
2024/10/5 数据结构打卡
java·数据结构·算法
结衣结衣.14 分钟前
C++ 类和对象的初步介绍
java·开发语言·数据结构·c++·笔记·学习·算法
学习使我变快乐16 分钟前
C++:静态成员
开发语言·c++
TJKFYY16 分钟前
Java.数据结构.HashSet
java·开发语言·数据结构
kylinxjd18 分钟前
spring boot发送邮件
java·spring boot·后端·发送email邮件
杰哥在此25 分钟前
Python知识点:如何使用Multiprocessing进行并行任务管理
linux·开发语言·python·面试·编程
小白学大数据26 分钟前
User-Agent在WebMagic爬虫中的重要性
开发语言·爬虫·http
OLDERHARD26 分钟前
Java - MyBatis(上)
java·oracle·mybatis