当前位置: 首页 > article >正文

Java基础——IO概述

IO概述

  1. io:输入和输出
    • 输入设备:
      • 键盘,鼠标,麦克风
    • 输出设备
      • 显示器,打印机,音响
  2. 为什么程序需要io
    • 做数据存储持久化。
  3. io流的分类(java.io 包下面的)
    • 按流向(输入和输出流)
    • 按数据单位(字节流字符流)
    • 四大基本流
      • 字节输入流
      • 字节输出流
      • 字符输入流
      • 字符输出流
    • 不管你是什么流,都需要关闭资源如果不关闭资源,磁盘文件会被占用,不能删除也不能修改

注意:操作IO流

  • 输入输出的目标搞明白,程序写出去,输出,程序读进来,输入
    • 输入
      • read()
    • 输出
      • write()

文件流

字节流

  1. 字节流:

    • FileInputStream 文件输入流

      • 需求:读 my.txt 的文件内容出来

        常用方法
        public FileInputStream(String name) throws FileNotFoundException {
            this(name != null ? new File(name) : null);
        }
        //通过文件获取输入流对象
        public FileInputStream(File file)
        //通过 byte[] 数组读
        public int read(byte b[]) throws IOException {
            return readBytes(b, 0, b.length);
        }
        //关闭资源的方式
        public void close()
          //获取文件对象
                File file = new File("D:\\javase\\homework\\practice\\day20\\my.txt");
                //获取文件输入流对象
                FileInputStream in = null;
                try {
                    in = new FileInputStream(file);
                    int length = -1;
        //        while ((length = in.read()) != -1){
        //            System.out.println(length);
        //        }
                    byte[] bytes = new byte[1024];
                    while ((length = in.read(bytes)) != -1){
                        String s = new String(bytes);
                        System.out.println(s);
                    }
        
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
        
    • FileOutputStream 文件输出流

      • 需求:把 sy666 写进刚才读的文件里面

        //append 表示是否追加
        public FileOutputStream(String name, boolean append)
        //关闭资源
        public void close()
        //写
         public void write(byte b[])
        
        //获取文件对象
                File file = new File("D:\\javase\\homework\\practice\\day20\\my.txt");
                FileOutputStream out = null;
                try {
                     out = new FileOutputStream(file,true);
                    String sy = "sy666";
                    byte[] bytes = sy.getBytes();
                    out.write(bytes);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    if(out != null){
                        try {
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
        
    • 需求:将 sy.txt 文件中的内容,拷贝成一个新文件 new.txt

      FileInputStream in = null;
              FileOutputStream out = null;
              try {
                  in = new FileInputStream("D:\\javase\\homework\\practice\\day20\\my.txt");
                  out = new FileOutputStream("D:\\javase\\homework\\practice\\day20\\new.txt");
                  byte[] buffer = new byte[10];
                  int length = -1;
                  // in.read 读数据到 buffer
                  while ((length = in.read(buffer)) != -1){
                      out.write(buffer);//把 buffer 中的数据写出去
                  }
      
      
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  try {
                      if (in != null) {
                          in.close();
                      }
                      if (out != null) {
                          out.close();
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
      
              }
      

字符流

  1. FileReader 输入

    FileReader reader = null;
    try {
        reader = new FileReader("D:\\javase\\homework\\practice\\day20\\my.txt");
        char[] buffer = new char[1024];
        int length = -1;
        while ((length = reader.read(buffer) ) != -1){
            String s = String.valueOf(buffer);
            System.out.println(s);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        reader.close();
    }
    
  2. FileWriter 输出

    FileWriter fileWriter = new FileWriter("D:\\javase\\homework\\practice\\day20\\my.txt", true);
    fileWriter.write("你好");
    fileWriter.write("上云");
    //关闭资源
    fileWriter.close();
    
  • 字节流与字符流的选择

    • 二进制文件,图片,音频,视频必须使用字节流
    • 文本文件(txt)使用字符流
    • 不清楚类型,用字节流
  • flush(刷新操作)

    • 计算机直接访问磁盘文件的时候,比操作内存慢,设置一个缓冲区,写的时候直接先写到内存,达到特定值再写到磁盘
    • 使用缓冲区意义:
      • 提高cpu的使用率
      • 回滚写入的数据
    • 操作系统使用 -1 代表磁盘文件结尾的标志
    • IO 是最影响程序性能的,缓冲区设置容量的整数倍,可以去有效的提高 io 性能 1024.

try catch 资源自动释放

  • 语法

    try(需要关闭资源的文件写到这里){
        
    }catch(){
        
    }
    
    public static void copyFile1(String srcFilePath, String destFilePath) {
    
            try (FileInputStream in = new FileInputStream(srcFilePath);
                 FileOutputStream out = new FileOutputStream(destFilePath);
                 
                 ) {
                byte[] buffer = new byte[10];
                int length = -1;
                // in.read 读数据到 buffer
                while ((length = in.read(buffer)) != -1) {
                    out.write(buffer);//把 buffer 中的数据写出去
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    
        }
    

缓冲流

  1. 缓冲流:其实就是个包装流,目的是起缓冲的作用,提升io 性能
  2. 字节缓冲流
    • BufferedInputStream 字节缓冲输入流
    • BufferedOutputStream 字节缓冲输出流
  3. 字符缓冲流
    • BufferedReader 字符缓冲输入流
    • BufferedWriter 字符缓冲输出流

使用缓冲流与不使用缓冲流性能的对比

  1. 使用单字节拷贝方式

    • 使用传统字节流

      public static void copy(String srcPath, String destPath) throws IOException {
              long begin = System.currentTimeMillis();
              FileInputStream in = new FileInputStream(srcPath);
              FileOutputStream out = new FileOutputStream(destPath);
              int length = -1;
              while ((length = in.read()) != -1) {
                  out.write(length);
              }
              in.close();
              out.close();
              long end = System.currentTimeMillis();
              System.out.println("拷贝耗时:" + (end - begin));
          }
      
    • 使用 Buffered 缓冲流

       public static void copyBuffer(String srcPath, String destPath) throws IOException {
              long begin = System.currentTimeMillis();
              FileInputStream in = new FileInputStream(srcPath);
              FileOutputStream out = new FileOutputStream(destPath);
              BufferedInputStream bufferedIn = new BufferedInputStream(in);
              BufferedOutputStream bufferedOut = new BufferedOutputStream(out);
      
              int length = -1;
              while ((length = bufferedIn.read()) != -1){
                  bufferedOut.write(length);
              }
              bufferedIn.close();
              bufferedOut.close();
              in.close();
              out.close();
              long end = System.currentTimeMillis();
              System.out.println("buffered 拷贝耗时:" + (end - begin));
          }
      
  2. 读和写外面再加buffer

    • 使用传统字节流

      long begin = System.currentTimeMillis();
      FileInputStream in = new FileInputStream(srcPath);
      FileOutputStream out = new FileOutputStream(destPath);
      int length = -1;
      byte[] buffered = new byte[10];
      while ((length = in.read(buffered)) != -1) {
          out.write(buffered,0,length);
      }
      in.close();
      out.close();
      long end = System.currentTimeMillis();
      System.out.println("copyBufferedBuffered拷贝耗时:" + (end - begin));
      
    • 使用缓冲流

      public static void copyBufferBuffered(String srcPath, String destPath) throws IOException {
              long begin = System.currentTimeMillis();
              FileInputStream in = new FileInputStream(srcPath);
              FileOutputStream out = new FileOutputStream(destPath);
              BufferedInputStream bufferedIn = new BufferedInputStream(in);
              BufferedOutputStream bufferedOut = new BufferedOutputStream(out);
              byte[] buffer = new byte[10];
              int length = -1;
              while ((length = bufferedIn.read(buffer)) != -1){
                  bufferedOut.write(buffer,0,length);
              }
              bufferedIn.close();
              bufferedOut.close();
              in.close();
              out.close();
              long end = System.currentTimeMillis();
              System.out.println("buffered buffer 拷贝耗时:" + (end - begin));
          }
      
    • 总结:使用缓冲流真的快

.write(buffer,0,length);
}
bufferedIn.close();
bufferedOut.close();
in.close();
out.close();
long end = System.currentTimeMillis();
System.out.println(“buffered buffer 拷贝耗时:” + (end - begin));
}
```

  • 总结:使用缓冲流真的快

http://www.kler.cn/news/284109.html

相关文章:

  • Java算法之快速排序(Quick Sort)
  • 服务器机柜与网络机柜的区别有哪些?
  • 耦合和内聚
  • redis集群部署
  • 集成电路学习:什么是DAC数模转换器
  • Maven <parent> 标签的作用及使用详解
  • 【React】useEffect的使用场景与作用
  • 什么软件可以用平板远程控制电脑?
  • 【使用 Python 进行图像裁剪的多种方法】
  • Leetcode Hot 100刷题记录 -Day5(双指针)
  • 1.7 离散频率
  • python学习-04【流程控制语句】
  • Qt 调用MFC dll,动态库中有界面
  • 数据结构——链式二叉树的实现与分治编程思维(c语言实现)
  • sql-labs靶场(41-50)
  • unity脚本
  • 理解 Maven 依赖范围及编译与运行时的需求
  • 无缝 CI/CD:如何在 Windows 环境中使用 Docker 和 Jenkins 自动化部署 .NET 应用
  • 嵌入式全栈开发学习笔记---Linux系统编程(进程控制)
  • 全球城市多边形和点数据集 (GUPPD)
  • 带你手撕面试题——定时器方案:红黑树版
  • OSINT技术情报精选·2024年8月中旬
  • 美容院拓客营销门店管理小程序渠道进行
  • 我的世界实体与生物ID表
  • 前后端传参@RequestParam使用上的一个小坑
  • 代码随想录八股训练营总结篇 2024年8月
  • 爬虫入门urllib 和 request (一)
  • Java+selenium 实现网页缩放的方法:用于解决页面太长部分元素定位不到的问题
  • 企业级NoSql数据库 --- Redis集群
  • Underactuated Robotics - 欠驱动机器人学(三)- 体操机器人、小车摆杆和四旋翼飞行器