javaIO流05:FileReader和Filewriter

FileReader

FileReader是字符流,按照字符来操作io

  1. FileReader的关系继承图

在这里插入图片描述
2. FileReader 相关方法

  • new FileReader(File/String)
  • read():每次读取单个字符,然后返回该字符,如果到文件末尾返回-1
  • read(chat[] ):批量读取多个字符到数组中,返回读取到的字符数,如果到文件末尾返回-1
  • new String(char[]):将数组char[]转换为String
  • new String(char[[],off,len):将数组char[]中从下标off开始往后数len个字符转换为String
  1. FileReader的使用案例
@Test
    public void Reader(){
        //读取的文件位置
        String filePath="D:/IOFile/news5.txt";
        FileReader fileReader=null;
          //定义一个int类型来接收read方法返回的ASCLL码
        int data = 0;
        try {
            fileReader = new FileReader(filePath);
           //read()方法读取一个字符返回一个字符,但这里的字符是以ASCLL码来表示的
            while (( data=fileReader.read())!=-1){
                //输出只需要将int类型的data转换为char
                System.out.print((char)data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void Reader2(){
        String filePath="D:/IOFile/news5.txt";
        FileReader fileReader=null;
        int readlen = 0;
        //以数组的方式进行读取,每次读取(new char[个数])个字符
        char[] buf=new char[8];
        try {
            fileReader = new FileReader(filePath);
            while (( readlen=fileReader.read(buf))!=-1){
                System.out.println((new String(buf,0,readlen)));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Filewriter

Filewriter是字符流,按照字符来操作io

  1. Filewriter的关系继承图

在这里插入图片描述
2. Filewriter 相关方法

  • new Filewriter(File/String):会覆盖以前的内容
  • new Filewriter(File/String,true):不会覆盖以前的内容,在以前内容最后进行写入
  • write(int):写入单个字符
  • write(char[]):写入指定数组
  • write(char[],off,len):写入数组char[]中从下标off开始往后数len个字符
  • write(String):写入整个字符串
  • write(String,off,len):写入中字符串从下标off开始往后数len个字符
  • 注意
    Filewriter在使用后必须调用close()方法关闭或者flush()方法刷新,否则写入不到指定的文件中去。
  1. Filewriter的使用案例
@Test
    public void Test(){
        String fileWPath="D:/IOFile/news6.txt";
        FileWriter fileWriter =null;
         char[] cha={'a','b','c'};
        try {
            fileWriter= new FileWriter(fileWPath);
            //write(int):写入单个字符
            fileWriter.write("h");

            //write(char[]):写入指定数组
            fileWriter.write(cha);

//write(char[],off,len):写入数组char[]中从下标off开始往后数len个字符
            fileWriter.write(cha,0,2);
//write(String):写入整个字符串
            fileWriter.write("hello,world");
//write(String,off,len):写入中字符串从下标off开始往后数len个字符
           fileWriter.write("hello,world!",11,1);
            System.out.println("写入字符成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //为什么FileWriter使用后必须关闭或者刷新,否则写入不到指定的文件中去
                //查看close源码
                //close-->se.close-->this.implClose()-->this.writeBytes();
                // writeBytes();直到这里才是将数据写入到文件中
                // 查看flush源码
                //flush--> se.flush();-->this.implFlush();--> this.implFlushBuffer();--> this.writeBytes();
                //发现close和flush最后都要进行writeBytes方法
                //所以如果不调用close或者flush就无法执行writeBytes方法,也就无法写入数据进入文件
                
                //fileWriter.close();
                 fileWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
  1. 为什么 Filewriter在使用后必须调用close()方法关闭或者flush()方法刷新

//为什么FileWriter使用后必须关闭或者刷新,否则写入不到指定的文件中去
//查看close源码
//close–>se.close–>this.implClose()–>this.writeBytes();
// writeBytes();直到这里才是将数据写入到文件中
// 查看flush源码
//flush–> se.flush();–>this.implFlush();–> this.implFlushBuffer();–> this.writeBytes();
//发现close和flush最后都要进行writeBytes方法
//所以如果不调用close或者flush就无法执行writeBytes方法,也就无法写入数据进入文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值