• 【第十三章 FilerReader读入数据,FileWrite写出数据,使用FilerReader和FileWrite实现文件文本复制】


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

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

    FileWriter//(字符流)
    FileReader
    FileInputStream//(字节流)
    FileOutputStream
    
    • 1
    • 2
    • 3
    • 4

    将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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    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();
                    }
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    /**
         * 对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
                      //  System.out.print(cbuffer[i]);
                    //}
                    //方式一错误写法
                    //for (int i=0;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();
                    }
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    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();
                    }
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    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();
                    }
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    哪些岗位需要考CDMP/CDGA/CDGP证书?
    863. All Nodes Distance K in Binary Tree
    联合教育部高等学校科学研究发展中心,阿依瓦科技创新教育专项正式发布!
    InstallAnywhere制作安装包
    LeetCode-77-组合
    【计算机扫盲】计算机的基础操作你知多少?
    一种用于干式脑电图的高密度256通道电极帽
    无需CORS,用nginx解决跨域问题,轻松实现低代码开发的前后端分离
    LINUX之进程管理
    【编程碎笔】-Java中关于next(),nextInt(),nextLine()的深度解剖
  • 原文地址:https://blog.csdn.net/qq_43742813/article/details/127542940