45 IO流

IO流

原理:

I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理设备之间的数据传输。如读/写文件,网络通讯等。 Java程序中,对于数据的输入/输出操作以“流(stream)”的方式进行。 java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。

 

流的分类:

  • 操作数据单位不同分为:字节流(8 bit),字符类流(16 bit)

  • 按数据流的流向不同分为:输入流,输出流

  • 按流的角色不同分为:节点流,处理流

 

 

抽象基类 字节流 字符类

输入流 InputSream Reader

输出流 OutputStream Writer

 

 

  1. 对于文本文件(.txt,.java,.c,.cpp),使用字符流处理

  2. 对于非文本文件(.jpg,.mp3,.avi,.mp4,.ppt,.....),使用字节流处理

FileReader

FileReader(char[] cbuf)

将day04的hello.txt文件内容读入程序中,并输出到控制台

说明点:

  1. read(): 返回读入的一个字符。如果达到文件末尾,返回-1

  2. 异常的处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally

  3. 读入的文件一定要存在,否则会报FileNotFoundException

        //1.实例化File类的对象,指明要操作的文件
       File file = new File("hello.txt");//相较于当前module
       //2.提供具体的流
       FileReader fr = new FileReader(file);

       //3.数据的读入
       //read(): 返回读入的一个字符。如果达到文件末尾,返回-1
//方式一:
       int date = fr.read();
       while (date != -1){
           System.out.print((char)date);
           date = fr.read();
      }

       //方式二:语法上针对于方式一的修改  
FileReader fr = null;
       try {
           //1.实例化File类的对象,指明要操作的文件
           File file = new File("hello.txt");//相较于当前module
           //2.提供具体的流
           fr = new FileReader(file);

           //3.数据的读入
           //read(): 返回读入的一个字符。如果达到文件末尾,返回-1
           //方式一:
//       int date = fr.read();
//       while (date != -1){
//           System.out.print((char)date);
//           date = fr.read();
//       }
//
           //方式二:语法上针对于方式一的修改
           int data;
           while((data = fr.read()) != -1){
               System.out.println((char)data);
          }
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           //4.流的关闭
           try {
               if(fr != null){
                   fr.close();
              }    
          } catch (IOException e) {
               e.printStackTrace();
          }
      }
       int data;
       while((data = fr.read()) != -1){
           System.out.println((char)data);
      }

       //4.流的关闭
       fr.close();

加强版

read(char[] cbuf):返回每次读入cbuf数组的字符个数。如果达到文件末尾,返回-1

        FileReader fr = null;
       try {
           //1.File类的实例化
           File file = new File("hello.txt");

           //2.FileReader流的实例化
           fr = new FileReader(file);

           //3.读入的操作
           //read(char[] cbuf):返回每次读入cbuf数组的字符个数。如果达到文件末尾,返回-1
           char[] cbuf = new char[5];
           int len;
           while((len = fr.read(cbuf)) != -1){
               //方式一:
               //错误的写法
//               for(int i =0; i < chuf.length ; i++){
//                   System.out.println(cbuf[i]);
//               }
//           }
               //正确的写法
//               for(int i =0; i < len ; i++){
//                   System.out.println(cbuf[i]);
//               }
               //方式二:
               //错误的写法:对应着方式一的写法
//               String str = new String(cbuf);
//               System.out.println(str);
               //正确的写法
               String str = new String(cbuf,0,len);
               System.out.println(str);

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

 

FileWriter

FileWriter(cbuf,0,len)

从内存中写出数据到硬盘的文件里

说明:

  1. 输出操作,对应的File可以不存在的,并不会报异常。

  2. File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。

    File对应的硬盘中的文件如果存在:

    如果流使用的构造器是:FileWriter(file,false) / FileWriter(file) :对原有文件的覆盖;

    如果流使用的构造器是:FileWriter(file,true):不会对原有文件基础上覆盖,而是在原有的文件上追加内容。

        @Test
       public void testFileWriter() throws IOException {
           //1.提供File类的对象,指明写出到的文件
           File file = new File("hello.txt");
           //2.提供FileWriter的对象,用于数据的写出
           FileWriter fw = new FileWriter(file,true);

           //3.写出的操作
           fw.write("你要加油啊");


           //4.流资源的关闭
           fw.close();
      }

 

通过FileReader和FileWriter实现文本的复制

   @Test
   public void testFileReaderFileWriter() {
       FileReader fr = null;
       FileWriter fw = null;
       try {
           //1.创建File类的对象,指明读入和写出的文件
           File srcFile = new File("hello.txt");
           File destFile = new File("hello1.txt");

           //2.创建输入流和输出流的对象
           fr = new FileReader(srcFile);
           fw = new FileWriter(destFile);

           //3.数据的读入和写出的操作
           char[] cbuf = new char[5];
           int len;//记录每次读入到cbuf数组中的字符的个数
           while((len = fr.read(cbuf)) != -1){
               //每次写出len个字符
               fw.write(cbuf,0,len);
          }
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           //方式一:
//           //4.关闭流资源
//           try {
//               if(fr != null)
//                   fr.close();
//           } catch (IOException e) {
//               e.printStackTrace();
//           }finally {
//               try {
//                   if(fw != null)
//                   fw.close();
//               } catch (IOException e) {
//                   e.printStackTrace();
//               }
//           }
//       }
           //4.关闭流资源
           //方式二:
           try {
               if(fr != null)
                   fr.close();
          } catch (IOException e) {
               e.printStackTrace();
          }

           try {
               if(fw != null)
                   fw.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }

      }

  }

 

 

FileInoutOutputStream

FileInputStream(byte[] buffer)

FileOutputStream(byte[] buffer,0,len)

实现对图片的复制

    @Test
   public void testFileInoutOutputStream() {
       FileInputStream fis = null;
       FileOutputStream fos = null;
       try {
           File srcfile = new File("uzi.jpg");
           File destfile = new File("uzi1.jpg");

           fis = new FileInputStream(srcfile);
           fos = new FileOutputStream(destfile);

           byte[] buffer = new byte[5];
           int len;
           while((len = fis.read(buffer)) != -1){
               fos.write(buffer,0,len);
          }
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if(fis != null){
               try {
                   fis.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if(fos != null){
               try {
                   fos.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }

          }
      }

  }

 

 

缓冲流

处理流之一:缓存流

BufferedInputStream(byte[] buffer)

BufferedOnputStream(byte[] buffer,0,len)

 

作用:提高流的读取,写入的速度

处理流就是“套接”在已有流的基础上

public class BufferedTest {

   @Test
   public void testBufferedStream() {
       BufferedInputStream bis = null;
       BufferedOutputStream bos = null;

       try {
           //1.造文件
           File srcFile = new File("uzi.jpg");
           File destFile = new File("uzi2.jpg");
           //2.造流
           //2.1 造节点流
           FileInputStream fis = new FileInputStream(srcFile);
           FileOutputStream fos = new FileOutputStream(destFile);

           //2.2 处理流
           bis = new BufferedInputStream(fis);
           bos = new BufferedOutputStream(fos);

           //3.复制的细节:读取,写入
           byte[] buffer = new byte[10];
           int len;
           while((len = bis.read(buffer)) != -1){
               bos.write(buffer,0,len);
               
               //bos.flush();//刷新缓冲区
          }
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           //4.资源的关闭
           //要求:先关闭外层的流,再关闭内层的流
          if(bos !=null){
              try {
                  bos.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }if(bis != null){
               try {
                   bis.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           //说明:关闭外层流的同时,内层流也会自动的进行关闭,关于内层流的关闭,我们可以省略
//       fos.close();
//       fis.close();
      }
       
  }

 

BufferedReader(char[] cbuf)

BufferedWriter(char[] cbuf,0,len)

实现对文本的复制

   @Test
   public void testBufferedWriter() {
       BufferedReader br = null;
       BufferedWriter bw = null;
       try {
           //创建文件和相应的流
           br = new BufferedReader(new FileReader(new File("hello.txt")));
           bw = new BufferedWriter(new FileWriter(new File("hello2.txt")));

           //读写操作
           //方式一:
//           char[] cbuf = new char[1024];
//           int len;
//           while((len = br.read(cbuf)) != -1){
//               bw.write(cbuf,0,len);
//               //bw.flush();
//           }
           //方式二:使用String
           String data;
           while((data = br.readLine()) != null){
               //方法一:
               //bw.write(data + "\n ");//不包含换行符
               //方法二:
               bw.write(data);
               bw.newLine();//提供换行的操作
          }


      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           //关闭资源
           if(bw != null){
               try {
                   bw.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }if (br != null){
               try {
                   br.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }

  }

 

 

图片的加密

  @Test
   public void test1() {
       FileInputStream fis = null;
       FileOutputStream fos = null;
       try {
           fis = new FileInputStream("uzi.jpg");
           fos = new FileOutputStream("uzisecret.jpg");

           byte[] buffer = new byte[20];
           int len;
           while ((len = fis.read(buffer)) != -1){
               //异或
               for(int i = 0; i < len;i++){
                   buffer[i] = (byte) (buffer[i] ^ 5);
              }
               fos.write(buffer,0,len);
          }
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if(fos != null){

               try {
                   fos.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }if(fis != null){

               try {
                   fis.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }

  }

 

posted @   flypiggg  阅读(137)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示