【第十三章 节点流-字符型(FilerReader,FileWriter)的使用,文件文本复制】

第十三章 节点流-字符型(FilerReader,FileWriter)的使用,文件文本复制

1.FilerReader读入数据操作
节点流:

FileWriter//(字符流)
FileReader
FileInputStream//(字节流)
FileOutputStream

将java senior下的hello.txt文件内容读入程序中,并输出到控制台。
①read()返回读入的字符,如果达到文件末尾,则返回-1。
②异常的处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理。
③读入的文件一定要存在,否则会报异常。

使用main方法
 public static void main(String[] args) {
        File file=new File("hello.txt");//相当于当前工程 C:\Users\26312\IdeaProjects\Java1\hello.txt
        System.out.println(file.getAbsolutePath());
        File file1=new File("java senior\\hello.txt");//C:\Users\26312\IdeaProjects\Java1\java senior\hello.txt
        System.out.println(file1.getAbsolutePath());
    }
1. @Test
    public  void testFleReader(){
        FileReader fileReader= null;
        try {
            //1.实例化File类的对象,指明要操作的文件
            File  file=new File("hello.txt");//相当于当前module下 C:\Users\26312\IdeaProjects\Java1\java senior\hello.txt
            System.out.println(file.getAbsolutePath());
            //2.提供具体的流
            fileReader = new FileReader(file);
            //3.数据的读入
            int data=fileReader.read();
            while (data!=-1){
                System.out.print((char)data);
                data=fileReader.read();
            }
        /*
        语法上的优化
        int data;
        while ((data=fileReader.read())!=-1){
            System.out.print((char)data);
        }*/
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileReader!=null){
                try {
                    //4.流的关闭操作
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
/**
     * 对read()方法的操作升级,使用read()的重载方法
     */
 2.   @Test
    public void testFileReader1(){
        FileReader fileReader= null;
        try {
            //1.File类的实例化
            File file=new File("hello.txt");
            //2.FileReader流的实例化
            fileReader = new FileReader(file);
            //3.读入的操作
            //read(char[] cbuffer):返回每次读入cbuffer数组中的字符的个数。如果达到文件末尾,返回-1
            char[] cbuffer=new char[5];
            int len;
            while ((len=fileReader.read(cbuffer))!=-1){
                //方式一:
                //for (int i=0;i<len;i++){
                  //  System.out.print(cbuffer[i]);
                //}
                //方式一错误写法
                //for (int i=0;i<cbuffer.length;i++){
                //  System.out.print(cbuffer[i]);
                //}
                //方式二:
                String str=new String(cbuffer,0,len);
                System.out.print(str);
                //方式二错误写法:和方式一错误写法结构一致
                //String str=new String(cbuffer);
                //System.out.print(str);
             }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileReader!=null){
                try {
                    //4.资源的关闭
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

2.FileWrite写出数据操作
从内存中写出数据到硬盘文件里。
说明:
①输出操作对应的File可以不存在的,并不会报异常。
②File对应的磁盘中的文件如果不存在,在输出的过程中,会自动创建此文件。
File对应的磁盘中的文件如果存在:
如果使用的构造器是:FileWriter(file,false)/FileWriter(file):对原有文件进行覆盖;
如果使用的构造器是:FileWriter(file,true):不会对原有文件进行覆盖,而是在原有文件上追加内容。

    @Test
    public void testFileWriter() throws IOException {
        FileWriter fileWriter= null;
        try {
            fileWriter = null;
            //1.提供File类的对象,指明写出到的文件
            File file=new File("hello1.txt");
            //2.提供FileWriter的对象,用于数据的写出
            fileWriter = new FileWriter(file,true);
            //3.写出的操作
            fileWriter.write("I have a dream!");
            fileWriter.write("You need have a dream!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileWriter!=null){
                try {
                    //4.资源流的关闭
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

3.使用FilerReader和FileWrite实现文件文本复制

 @Test
    public void testFileReaderWriter() {//不能使用字符流来处理图片文件等字节数据
        FileReader fileReader= null;
        FileWriter fileWriter= null;
        try {
            //1.创建File的对象,指明读入和写出的文件
            File srcFile=new File("hello.txt");
            File destFile=new File("hello2.txt");
            //2.创建输入流和输出流的对象
            fileReader = new FileReader(srcFile);
            fileWriter = new FileWriter(destFile);
            //3.数据的读入和写出操作
            char[] cbuffer=new char[5];
            int len;//记录每次读入到cbuffer字符数组中的字符的个数
            while ((len=fileReader.read(cbuffer))!=-1){
                //每次写出len字符
                fileWriter.write(cbuffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileWriter!=null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //4.关闭资源流
            if(fileReader!=null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值