Java IO流--使用FileReader字符输入流读入数据到java程序或者内存的基本操作

博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,
👉点击跳转到网站

前言:

1、流的分类:

  • 1.操作数据单位:字节流、字符流
  • 2.数据的流向:输入流、输出流
  • 3.流的角色:节点流、处理流

2、 流的体系结构:

二、流的体系结构
抽象基类         节点流(或文件流)                               缓冲流(处理流的一种)
InputStream     FileInputStream   (read(byte[] buffer))        BufferedInputStream (read(byte[] buffer))
OutputStream    FileOutputStream  (write(byte[] buffer,0,len)  BufferedOutputStream (write(byte[] buffer,0,len) / flush()
Reader          FileReader (read(char[] cbuf))                 BufferedReader (read(char[] cbuf) / readLine())
Writer          FileWriter (write(char[] cbuf,0,len)           BufferedWriter (write(char[] cbuf,0,len) / flush()

在这里插入图片描述
FileReader的相关方法如下:
在这里插入图片描述
注意点:

  1. read()方法的理解:返回读入的一个字符,如果达到文件的末尾,则返回-1
  2. 异常的处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理
  3. 读入程序中的文件一定要存在,否则会报FileNotFoundException异常

使用一:

public class FileTest1 {
    @Test
    public void test1(){
        FileReader fr=null;
        try {
            //1.实例化file类的对象,指明要操作的文件
            File file = new File("hello.txt");
            //2.提供具体的流
            fr = new FileReader(file);
            //3.数据的读入
            //read():返回读入的一个字符,如果达到文件的末尾,则返回-1
            int data;
            while ((data=fr.read())!=-1){
                System.out.print((char)data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr!=null) {
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

输出结果:

abcdef

二、对read()方法进行操作升级,使用read的重载方法

 @Test
    public void FileReaderTest(){
        FileReader fr = null;
        try {
            //1.实例化File类的对象
            File file = new File("hello.txt");
            //2.提供具体的流 FileRead流的具体实例化
            fr = new FileReader(file);
            //3.数据的读入
            int len;
            char[] cbuf = new char[5];
            //read(char cbuf[]):返回每次读入cbuf数组中的字符的个数,如果达到文件末尾,则返回-1
            while ((len=fr.read(cbuf))!=-1){
                for (int i = 0; i <len ; i++) {
                    System.out.print(cbuf[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4.资源的关闭
                if (fr!=null) {
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

输出结果:

abcdefhigklmn

使用二:

public class FileReader_ {
    /**
     * 单个字符读取文件
     */
    @Test
    public void fileReader01() {
        String filePath = "D:\\story.txt";
        FileReader fileReader = null;
        int data = 0;
        try {
            fileReader = new FileReader(filePath);
            //循环读取使用read,单个字符读取
            while ((data = fileReader.read()) != -1) {
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 使用字符数组读取文件
     */
    @Test
    public void fileReader02() {
        String filePath = "D:\\story.txt";
        FileReader fileReader = null;
        int readLen = 0;
        char[] buf = new char[8];
        try {
            fileReader = new FileReader(filePath);
            //循环读取使用read(buf),返回的是实际读取到的字符数
            //如果返回-1,说明文件结束
            while ((readLen = fileReader.read(buf)) != -1) {
                System.out.print(new String(buf, 0, readLen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

路宇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值