美文网首页
Java--io流及异常机制

Java--io流及异常机制

作者: _Raye | 来源:发表于2016-12-26 23:36 被阅读0次
    • 遗留容器(自己写代码不要用但是可能在项目早期版本中能遇到的容器)
      所谓的遗留容器,就是在新版本中已经被淘汰的容器 - 特点:设计烂,效率低,使用麻烦
      BitSet -->二进制位集合
      Vector --> ArrayList的线程安全版本
      Stack --> 栈:先进后出的容器(FILO容器)
      Dictionary -->字典(键值对容器)
      Hashtable --> HashMap的线程安全版本
      Properties --> 键和值的类型都是string的HashMap(映射)

    IO流

    • Java - IO操作被抽象成了流
      只有一个方向一个维度
      读数据 - 输入流 - read()
      写数据 - 输出流 - write()
    • FileInputStream的构造器用throws关键字声明了FileNotFoundException异常
      而且该构造器的代码中判定如果文件无效就用throws关键字抛出异常对象
    public static void main(String[] args) {
            InputStream in = null;
            try {
                in = new FileInputStream("c:/MyJava/常用正则表达式.txt");
                int data;
                while ((data = in.read()) != -1) {
                    System.out.printf("%x ",data);
                }
            } 
            catch (FileNotFoundException e) {
                e.printStackTrace();    
            } 
            catch (IOException e) {
                e.printStackTrace();
            }
            finally{
                if (in != null) {
                    try {
                        in.close();
                    } 
                    catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    • 如果频繁的进行I/O操作会使得CPU的利用率非常低下
      因为I/O操作会导致CPU产生I/O中断,CPU不能满负荷工作

    • 创建一个字节数组作为输入缓冲区,将文件中的数据直接读入缓冲区
      这种做法可以减少I/O中断的次数从而保证cpu能够不被频繁中断

    byte[] buffer = new byte[1024];
                int totalBytes;  //记录read操作总共读到了多少字节
                while ((totalBytes = in.read(buffer)) != -1){
                    System.out.print(new String(buffer,0,totalBytes));
              } 
    
    • InputStream - 字节流(以字节为单位进行读取) - 二进制文件
      Reader - 字符流(以字符为单位进行读取) - 文本文件

    • FileWriter构造器的第一个参数是文件名或者File对象
      第二个参数用来指定是否启动追加模式(在原来文件的末尾输出新的内容)

    try(Writer writer = new FileWriter("c:/Users/apple/Desktop/abc.txt",true)){//true - 追加模式
                writer.write(s1 + "\r\n");//回车换行
                writer.write(s2 + "\r\n");
            }
            catch (IOException e) {
                e.printStackTrace();
            }
    

    java异常机制

    • try:在运行时可能出异常状况的代码放在里面保护起来,如果发生异常就通过后面的catch来捕获异常对象并进行相应的处理
    • 一个try块后面可以跟多个catch用于捕获不同的异常类型
      但是在书写上要保证先捕获子类异常再捕获父类型异常
      如果捕获的异常之间没有父子关系,那么捕获的顺序随意
      从Java 7 开始,如果多个catch代码是一致的,可以合并为一个catch(保证两个异常没有父子关系)
    • finally - 总是执行代码块 - 不管程序正常异常,此处都要执行
      此处最适合释放程序中打开的各种外部资源(文件,数据库,网络)
    public static void main(String[] args) {
            InputStream in = null;
            try {
                in = new FileInputStream("c:/MyJava/常用正则表达式.txt");
                int data;
                while ((data = in.read()) != -1) {
                    System.out.printf("%x ",data);
                }
            } 
            catch (FileNotFoundException e) {
                e.printStackTrace();    
            } 
            catch (IOException e) {
                e.printStackTrace();
            }
            finally{
                if (in != null) {
                    try {
                        in.close();
                    } 
                    catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    • 从Java 7 开始可以使用TWR语法,将需要释放的外部资源直接放在try后的圆括号中
      这样的话不管是正常离开还是异常离开try块,资源会自动关闭
      实现了AutoCloseable接口的外部资源对象可以使用TWR语法自动关闭
    try(InputStream in = new FileInputStream("c:/MyJava/常用正则表达式.txt")) {            
                byte[] buffer = new byte[1024];
                int totalBytes; 
                while ((totalBytes = in.read(buffer)) != -1){
                    System.out.print(new String(buffer,0,totalBytes));
              }         
            }
            catch (FileNotFoundException e) {
                e.printStackTrace();    
            } 
    

    相关文章

      网友评论

          本文标题:Java--io流及异常机制

          本文链接:https://www.haomeiwen.com/subject/dodsvttx.html