重学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();
        }
    }
}
相关推荐
方也_arkling1 小时前
【Java-Day08】static / final / 枚举
java·开发语言
橙淮1 小时前
Spring Bean作用域与生命周期全解析
java·spring
风吹夏回1 小时前
Python 全局异常处理:从“满屏 try-except”到优雅兜底
开发语言·python
Chengbei112 小时前
一站式源码安全检测工具、云安全 / APP / 小程序源码敏感信息递归多层目录扫描AK、JWT、手机号、身份证等敏感信息
java·开发语言·安全·web安全·网络安全·系统安全·安全架构
llz_1122 小时前
web-第一次课后作业
java·开发语言·idea
小熊Coding2 小时前
Python爬取当当网二手图书项目实战!
开发语言·爬虫·python·beautifulsoup·requests·二手图书
秋92 小时前
Java项目运行5天左右自动宕机:系统性定位与解决方案
java·开发语言·python
小江的记录本2 小时前
【JVM虚拟机】垃圾回收GC:垃圾收集器:CMS:核心原理、回收流程、优缺点、废弃原因(附《思维导图》+《面试高频考点清单》)
java·jvm·后端·python·spring·面试·maven
xiaoshuaishuai82 小时前
C# 内存管理与资源泄漏
开发语言·c#
DIY源码阁2 小时前
JavaSwing学生成绩管理系统 - MySQL版
java·数据库·mysql·eclipse