学习Java的第14天

学习Java的第14天

大纲

  • IO流的原理及流的分类
  • 节点流(文件流)
  • 缓冲流(处理流的一种)
  • 转换流
  • 标准输入输出流
  • 打印流
  • 数据流
  • 对象流
  • 随机存取文件流
  • NIO.2中的path pathsFile类的使用
  1. IO流原理及流的分类

    1. io流原理

    • input:将文件中的数据读取到程序中。
    • output:程序的数据输出到文件当中。
    • 流(stream):流就是一个水管 接通文件 和 程序。
    1. 流的分类

    • 按照数据单位区分为 字节流 和字符流
      • 字节流用于传输图片和视频
      • 字符流用于传输文本信息
    • 按照数据的流向可以分为 输入流和输出流
    • 按照角色可以分为 节点流 和 处理流
      • 从一个特定的数据源(节点)读写数据。
      • 从通过一个间接流类去调用节点流,以达到更加灵活的读写各种数据。
    • io流中有四个抽象基类
      • inputStream outputStream用于处理字节的输入输出

      • reader writer 用于处理字符的输入输出

      • 他们的子类看结尾就可以看出是处理字节还是字符

/*
文本文件 写入写出操作
 */
    @Test
    public void testFileRw(){
        FileReader fr1 = null;
        FileWriter fw2 = null;
        try {
            //1.创建写入文件的操作对象
            File f1 = new File("hello");
            File f2 = new File("hello1");
            //2.创建输入输出流
             fr1 = new FileReader(f1);
             fw2 = new FileWriter(f2,true);//true表示不是覆盖 在原有增加
            //3.执行读取写入操作
            char[] chars = new char[5];
            int len;
            while ((len = fr1.read(chars)) != -1){
                fw2.write(chars,0,len);
                //4.遍历写入 当前数组长度的字符
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fr1 != null) {//加上这个是判断fr1这个流是否是被使用 没有就不用区关闭了
                try {
                    fr1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }
            }
            if(fr1 != null) {
                try {
                    fw2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }
            }
          
        }
    }
}

 @Test
    /*
    照片视频的写入写出操作
     */
    public void inputOutputTest() throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //1.创建文件对象
            File file1 = new File("123.jpg");
            File file2 = new File("456.jpg");
            //2.创建输入输出流
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            //3.复制的过程
            byte[] bytes = new byte[5];
            int len;
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                  try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            }
          
             if(fos != null){
                  try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            }
        }
    }

  1. 缓冲流BufferStream

    1. 缓冲流的使用

      • BufferedInputStream
      • BufferedOutputStream
      • BufferedReader
      • BufferedWriter
    2. 作用:

      1. 提高流的读取 写入速度
      2. 原因:内部提供了一个缓冲区
    3. 处理流:就是套接在已有的流的基础上

    4. 举例

      {
              BufferedInputStream bis = null;
              BufferedOutputStream bos = null;
              try {
                  //1.创建文件对象
                  File file1 = new File("123.jpg");
                  File file2 = new File("456.jpg");
                  //2.创建输入输出流
                  FileInputStream fis = new FileInputStream(file1);
                  FileOutputStream fos = new FileOutputStream(file2);
                  //2.1将节点流的对象放入缓冲流的参数列表
                  bis = new BufferedInputStream(fis);
                  bos = new BufferedOutputStream(fos);
                  //3.复制的过程
                  byte[] bytes = new byte[5];
                  int len;
                  while ((len = fis.read(bytes)) != -1) {
                      fos.write(bytes, 0, len);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
                  //缓冲流 可以只关闭外层流 默认关闭内层流 
                  //关闭顺序 先关闭外层 再关闭内层
              } finally {
                  if (bos!= null) {
                      try {
                          bos.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      } finally {
                      }
                  }
                  if (bis!= null) {
                      try {
                          bis.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      } finally {
                      }
                  }
              }
          }
      
  2. 转换流

    1. 定义:
    • InputStreamReader
      • 实现将字节的输入流按指定字符集转换为字符的输入流。
    • OutputStreamWriter
      • 实现将字符的输出流按指定字符集转换为字节的输出流。
    1. 解码:将字节转换为字符。

      编码:将字符转换为字节。

    2. 字符集:

      • utf-8:可变长 汉字占3个字节

      • gbk:所有均为两个字节 在首位来区分是一个字节的字符

        还是两个字节的字符。

       @Test
          public void ISROSW(){//两种转换流的缩写
              //1.创建文件
              InputStreamReader isr = null;
              OutputStreamWriter osw = null;
              try {
                  File file1 = new File("hello1");
                  File file2 = new File("hello2gbk.txt");
                  //2.创建节点流
                  FileInputStream fis = new FileInputStream(file1);
                  FileOutputStream fos = new FileOutputStream(file2);
                  //3.进行指定字符集的转换的输入输出流
                  isr = new InputStreamReader(fis,"UTF-8");
                  osw = new OutputStreamWriter(fos,"GBK");
                  //4.进行指定格式的复制
                  char[] chars = new char[10];
                  int len;
                  while ((len = isr.read(chars)) != -1 ){
                      osw.write(chars,0,len);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
              try {
                  isr.close();
                  osw.close();
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
              }
      
      
          }
      
  3. 标准的输入输出流(了解)

    • system in:默认从键盘输入

    • system out:默认从控制台输出

    • 通过setin(pintStream in) 和setout (printStream out)

    • 对默认输出做出改变

      /*
      在键盘输入字符串,要求将读取到的整行字符串转换为大写输出 然后继续进行输入操作
      将标准输入流 转换为字节流 再通过缓冲流
       */
      public class OtherStream {
          public static void main(String[] args) {
              BufferedReader bis = null;
              try {
                  //将标准的键盘输入流用转换流转换为字符输入流
                  InputStreamReader isr = new InputStreamReader(System.in);
                  bis = new BufferedReader(isr);
                  //将字符输入流加上字符缓冲流的处理流
      //        BufferedInputStream bis2 = new BufferedInputStream(isr)
                  //将字符处理流加上字节缓冲流的处理流
                  char[] chars = new char[10];
                  int len;
                  while (true){
                      String data = bis.readLine();
      
                      if ("e".equalsIgnoreCase(data)||"exit".equalsIgnoreCase(data)){
      
                          System.out.println("程序关闭");
                          break;
                      }
                      System.out.println(data.toUpperCase());
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  if (bis != null){
                      try {
                          bis.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      } finally {
                      }
                  }
              }
          }
          }
      
      
      
  4. 打印流(了解)

    • 实现将基本数据类型数据格式转化为字符串输出

      • PrintStream 和 Printwriter

      • 提供了以一系列重载的print 和println()实现对不同基本数据类型格式化转化为字符串输出。

          /*
            保存指定的打印输出流,使其保存到文本当中
             */
            public static void main(String[] args) {
                java.io.PrintStream ps = null;
                try {
                    FileOutputStream fos = new FileOutputStream("E:\\2d.txt");
                    ps = new java.io.PrintStream(fos, true);
                    if (ps != null) {
                        System.setOut(ps);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i <255 ; i++) {
                    System.out.print((char)i);
                    if (i %20 == 0){
                        System.out.println();
                    }
                }
            }
        
  5. 数据流(了解)

  • DataInputStream 和DataOutPutStream

  • 分别套接在InputStream 和OutPutStream子类的流上

    public class DataStreamTest {
        /*
        写入指定的基本数据类型
        读取指定的数据类型
        写入 和 读取的 顺序要一致
         */
        @Test
        public void test1() {
            DataOutputStream dos = null;
            try {
                dos = new DataOutputStream(new FileOutputStream("hello"));
                dos.writeInt(23);
                dos.flush();
                dos.writeUTF("叫花鸡");
                dos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (dos != null) {
                    try {
                        dos.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                    }
                }
            }
    
        }
    
        @Test
        public void test2() {
            DataInputStream dis = null;
            try {
                dis  = new DataInputStream(new FileInputStream("hello"));
                int age = dis.readInt();
                System.out.println("年龄:"+age);
                String name = dis.readUTF();
                System.out.println("姓名:"+name);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (dis != null){
                    try {
                        dis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                    }
                }
            }
    
        }
    }
    
    

猜你喜欢

转载自www.cnblogs.com/wumingzhibei/p/12585705.html