JavaIO流1

JavaIO流1

处理设备之间的数据传输,如:读/写文件,网络通讯等

Java中,对于数据的输入/输出操作以"流(stream)"的方式进行

IO流原理(我们站程序的一方)

  • 输入input:读取外部数据到程序(内存)中
  • 输出output:将程序数据输出到磁盘等设备

流的分类

  • 按操作数据单位的不同:字节流(8 bit)(图片,视频)、字符流(16 bit)(文本)
  • 按数据流的流向不同:输入流、输出流
  • 按流的角色不同:节点流(直接作用在文件上)、处理流

IO流的体系结构

  1. 对于文本文件(txt,.java,.c,.cpp):字符流
  2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc...):字节流
抽象基类                  节点流(文件流)               缓冲流(处理流的一种)
InputStream              FileInputStream             BufferedInputStream
OutputStream             FileOutputStream            BufferedOutputStream
Reader                   FileReader                  BufferedReader
Writer                   FileWriter                  BufferedWriter

FileReader FileWriter的使用

  1. 创建File类的对象
  2. 创建流的对象
  3. 数据的读入与写出
  4. 关闭流

FileReader

     public static void text1()  {
         //将src下的hell文件读入程序,并输出到控制台
         /*
         * 1.read()的理解:回读入的一个字符。如果到文件末尾,返回-1
         * 2. 异常的处理:为保证流资源一定可以执行关闭,使用try-catch-finaLL(选中所选内容Ctrl+Alt+t)
         * 3.读入的文件一定要存在
         * */
         FileReader fr = null;
         try {
             //1.实例化File对象,指明操作的文件
             File file = new File("src\\hello");//相较于当前Module
             //2.提供具体的流:FileReader
             fr = new FileReader(file);
             //3.数据的读入
             int date = fr.read();
             while (date != -1) {
                 System.out.print((char) date);
                 date = fr.read();
             }
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
         }
         //4.流的关闭
         try {
             if (fr != null)//如果没有实例化
                 fr.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
         System.out.println();
     }


     //对read()方法的升级:使用read的重载方法
     public static void text2()  {
         FileReader fr = null;
         try {
             //1.File类的实例化
             File file = new File("src\\hello");
             //2.FileReader流的实例化
             fr = new FileReader(file);
             //3.读入操作
             /*  read(数组):每次读入数组中的资格个数。如果到文件末尾,返回-1  */
             char[] cbuf =new char[5];
             int len = fr.read(cbuf);//实际放入的个数(有几个,就是几)
             while(len != -1) {
                 //方式一
     //            for (int i = 0; i < len; i++) {
     //                System.out.print(cbuf[i]);
     //            }
                 //方式2
                 String str = new String(cbuf,0,len );
                 System.out.print(str);
                 len = fr.read(cbuf);
             }
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
         }
         //4.资源(流)的关闭
         try {
             if(fr != null)
                 fr.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
         System.out.println();
     }

输出hello文件的"helloworld"

FileWriter

//FileWriter
     public static void text3(){
         //从内存中写出数据到硬盘文件里
         /**
          * 说明:
          *   输出操作:file可以不存在,系统自动创建
          *            file存在,FileWriter(file,false)或是FileWriter(file):覆盖原文件 ;
          *                    FileWriter(file,true):在原文件基础上添加
          */FileWriter fw = null;
         try {
             //1.提供file的对象,指明写出到的文件
             File file = new File("src\\hello1");
             //2.提供FileWriter的对象,用于数据写出
             fw = new FileWriter(file);
             //3.写出操作
             fw.write("I have a dream\n");
             fw.write("I do not have a dream");
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
         }
         //4.流的关闭
         try {
             if (fw != null)
                 fw.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }

再hello1文件中写出I have a dream(换行)I do not have a dream

文本文件的复制

//文本文件的复制
    public static void text4() {
        //读进来,再写出去
        FileReader fr = null;
        FileWriter fw = null;

        try {
            //1.创建File类的对象,读入与写出
            File comrfile = new File("src\\hello");
            File gofile = new File("src\\hello2");
            //2.创建流的对象,输入与输出
            fr = new FileReader(comrfile);
            fw = new FileWriter(gofile);
            //3.数据的读入与写出
            char[] arr = new char[5];
            int len = fr.read(arr);
            while(len != -1){
                for (int i = 0; i < len; i++) {
                    fw.write(arr[i]);
                }
                len = fr.read(arr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }

        //4.关闭资源流
        try {
            if(fw != null)
                fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(fr != null)
                fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

hello文件中的"helloworld"复制到hello2中

FileInputStream FileOutputStream的使用

与FileReader FileWriter操作类似

//指定的文件
public void copy(String srcPath,String destPath){//两个形参
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File comefile = new File(srcPath);
            File gofile = new File(destPath);

            fis = new FileInputStream(comefile);
            fos = new FileOutputStream(gofile);

            byte[] buffer = new byte[1024];
            int len = fis.read(buffer);
            while (len != -1) {
                fos.write(buffer,0,len);
                len = fis.read(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    
        try {
            if(fos != null)
                fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(fis != null)
                fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
posted @ 2022-02-16 17:50  T,a,o  阅读(42)  评论(0编辑  收藏  举报