• 输入输出操作(二)


    一,常用流
      1,字节流
        概述:一切皆为字节,byte,计算机最小的存储单元。图片,视频以二进制的方式存储,一个一个字节,所以字节流可以读任何文件。
        1》字节输出流
            java.io.OutputStream所有字节输出流的超类 ----》子类 FileOutputStream 文件字节输出流
          构造:
            FileOutputStream(File name);
            FileOutputStream(String path);
          方法:
            close(); 关流
            flush(); 刷新
            write(byte[ ] byet); 写入
          步骤:
            1,创建FileOutputStream对象,构造器中绑定要写出的数据源(文件地址)
            2,使用对象.write(); 写文件
            3,释放资源,关流(先开的后关,后开的先关)
        2》字节输入流
            java.io.InputStream所有字节输入流的超类 ----》子类 FileInputStream 文件字节输入流
          构造:
            FileInputStream(File name);
            FileInputStream(String path);
          方法:
            read(); 读取下一个字节
            read(byte[ ] bytes); 一次读取多个字节,参数字节数组起到缓冲作用, 长度为1024b=1kb
            close(); 关流
          步骤:
            1,创建FileInputStream对象,构造器中绑定要读取的数据源(文件地址)
            2,使用对象.read(); 读文件
            3,释放资源,关流(先开的后关,后开的先关)

      2,字符流
        1》字符输入流
            java.io.Reader字符输入流的顶层父类----》子类InputStreamReader----》子类FileReader 文件字符输入流
          构造:
            FileReader(File name);
            FileReader(String path);
          方法:
            read(int len);
            read(char[ ] chars);
            read(String str,int off,int len);
            close();
          步骤:
            1,创建FileReader对象,构造器中绑定要读取的数据源
            2,使用对象.read(); 读取数据
            3,释放资源
        2》字符输出流
            java.io.Writer字符输出流的超类----》子类OutputStreamWriter----》子类 FileWriter文件字符输出流
          构造:
            FileWriter(File name);
            FileWriter(String path);
          方法:
            write(int len);
            write(char[ ] chars);
            write(String str,int off,int len);
            flush();
            close();
          步骤:
            1,创建FileWriter对象,构造器中绑定要读取的数据源
            2,使用对象.write();---- 写出数据到缓冲区
            3,对象.flush();----将缓冲区数据写到文件中
            4,释放资源
         ★,flush与close区别
            flush();刷新流对象,强制清理缓冲区,不关流可以继续使用
            close();刷新缓冲区,然后通过释放资源方式关流,流不能再使用
         续写,换行:
            FileWriter(String fileName,boolean append); 追加续写true,false不成功
            换行符: Windows  
                  Linux   /n
                  mac   /r

    二,流异常处理
      JDK7之前,使用try,catch,finally处理流中的异常
        try{
          可能异常代码;
        }catch(异常名){
          异常处理操作;
        }finally{
          都会执行的代码;
        }
      JDK9新特性:
        1,try前面可以定义流对象
        2,在try后面()中可以直接引入流对象的名称(变量名)
        3,在try代码执行完毕以后,流对象可以直接释放,不用写finally

    三,Properties (重点)
      概述:java.util.Properties集合,----》extends HashTable<k,v> implements Map<k,v>表示一个持久的属性集合,Properties可以保存在流中,或从流中加载,是唯一一个和
    IO流向结合的集合。
      方法:
        store(); 把流中的临时数据,持久化到硬盘中存储
        load(); 把硬盘中的文件(键值对)读取到集合中使用
      ★使用Properties集合存储数据,遍历取出
        setProperties(String key,String value); 设置键值
        getProperties(String key); 根据key获取value值
        stringPropertiesNames();----》 相当于 keySet 方法,把key保存到一个新集合中

    四,缓冲流
      概述:BufferedStream用来提高读写的效率。
        1,字节缓冲输出流
            BufferedOutputStream----》extends OutputStream
          构造:
            new BufferedOutputStream(OutputStream os);
          方法:
            write();
            flush();
            close();
          步骤:
            1,创建FileOutputStream对象,绑定文件目的地
            2,创建BufferedOutputStream对象,包裹FOS对象
            3,使用BufferedOutputStream对象.write();写到缓冲区
            4,使用BufferedOutputStream对象.flush();将缓冲去数据写到文件中
            5,释放资源
        2,字节缓冲输入流
            BufferedInputStream----》extends InputStream
          构造:
            new BufferedInputStream(InputStream is);
          方法:
            read();
            close();
          步骤:
            1,创建FileInputStream对象,绑定文件目的地
            2,创建BufferedInputStream对象,包裹FIS对象
            3,使用BufferedInputStream对象.read();读取数据
            4,释放资源

    如:
    package cn.kgc.demo01;
    import java.io.*;
    /**
    * Created by ZhaoQiannan on 2019/5/26 17:18
    */
    public class CopyTest {
      // 主方法
      public static void main(String[] args) {
        try {
          io();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
        // 缓冲流读写
      private static void io() throws IOException {
        // 开始时间
        long start = System.currentTimeMillis();


        FileInputStream fis=null;
        BufferedInputStream bis=null;
        FileOutputStream fos=null;
        BufferedOutputStream bos=null;
        try {
          // FileInputStream对象
          fis = new FileInputStream("e:\Test\mm.jpg");
          // BufferedInputStream对象
          bis=new BufferedInputStream(fis);
          // FileOutputStream对象
          fos=new FileOutputStream("e:\Test\mv.jpg");
          // BufferedOutputStream对象
          bos=new BufferedOutputStream(fos);
          // 定义数组,读写数据
          byte[] b=new byte[1024];
          int len=0;
          while ((len=bis.read(b))!=-1){
            bos.write(b,0,len);
            bos.flush();
          }
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } finally {
          // 释放
          try {
            bos.close();
            fos.close();
            bis.close();
            fis.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        // 结束时间
        long end = System.currentTimeMillis();
        //所用时间
        long seconds = end - start;
        System.out.println("复制所用时间"+seconds);

      }
    }

  • 相关阅读:
    城市承灾体脆弱性和易损性的影响因素
    《风暴潮、海浪、海啸和海冰灾害应急预案》
    承灾体
    ArcGIS数据存储的方式
    ArcGIS几种数据格式2
    ArcGIS几种数据格式
    【ArcGIS】文件地理数据库,个人地理数据库与ArcSDE的局别
    dojo事件绑定
    Spark最简安装
    Spark 概述
  • 原文地址:https://www.cnblogs.com/kide1412/p/10929654.html
Copyright © 2020-2023  润新知