当前位置: 移动技术网 > IT编程>开发语言>Java > J01-Java IO流总结一 《异常捕获》

J01-Java IO流总结一 《异常捕获》

2018年09月15日  | 移动技术网IT编程  | 我要评论

好学力行bbs,我的女儿瑞英国语版,叙利亚遭战机投毒气

下面演示java中处理i/o操作时的异常的正确方式。

 

先看一种不正确的方式

方式一:

import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

public class test3 {
    public static void main(string[] args) {
        fileinputstream fis = null;
        byte[] buffer = new byte[1024];
        int temp = 0;
        
        //打开文件
        try {
            fis = new fileinputstream("e:/test_file/a.txt");
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        }
        
        //访问文件
        try {
            while(-1 != (temp = fis.read(buffer))) {
                system.out.print(new string(buffer, 0, temp));
            }
        } catch (ioexception e) {
            e.printstacktrace();
        }
        
        //关闭文件
        try {
            fis.close();
        } catch (ioexception e) {
            e.printstacktrace();
        }
        
    }
}

 注意到前面的代码在读取文件的try代码块完成后关闭了文件流,虽然这种方法在某些情况下有用,但是java提供了一种在通常情况下更好的方法。即,在finally代码块中调用close()在这种方法中,访问文件的所有方法都包含在一个try代码块中,finally代码块用来关闭文件这样,无论try代码块如何终止,文件都会被关闭。使用前面的示例,下面演示了如何重新编码读取文件的try代码块:

方式二:

import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

public class test4 {
    public static void main(string[] args) {
        fileinputstream fis = null;
        byte[] buffer = new byte[1024];
        int temp = 0;
        
        //打开文件
        try {
            fis = new fileinputstream("e:/test_file/a.txt");
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        }
        
        //访问文件
        try {
            while(-1 != (temp = fis.read(buffer))) {
                system.out.print(new string(buffer, 0, temp));
            }
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            //关闭文件
            try {
                fis.close();
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }
}

 

这种方法的优点之一是,如果访问文件的代码由于某种与i/o无关的异常而终止,finally代码块仍然会关闭文件。虽然在这个例子中这不是一个问题,因为在发生未预料到的异常时程序简单地结束了,但是在大型的程序中却可能造成很多麻烦。使用finally可以避免这些麻烦

有时,将程序中打开文件和访问文件的部分放到一个try代码块,然后使用一个finally代码块关闭文件,这样更加简单。例如,下面是另一种编写方式:

方式三(推荐方式!):

import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

public class test4 {
    public static void main(string[] args) {
        fileinputstream fis = null; //先初始化为null
        byte[] buffer = new byte[1024];
        int temp = 0;
        
        try {
            //打开文件
            fis = new fileinputstream("e:/test_file/a.txt");
            
            //访问文件
            while(-1 != (temp = fis.read(buffer))) {
                system.out.print(new string(buffer, 0, temp));
            }
            
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            //关闭文件
            try {
                if(null != fis) {   //非空判断
                    fis.close();
                }
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }
}

 在这种方法中,注意fis被初始化为null。然后,在finally代码块中,只有fis不为null时才关闭文件。可以这么做的原因是,只有文件被成功打开,fis才不为null。因为,如果在打开文件的过程中出现异常,就不应调用close()方法,否则会报nullpointerexception。推荐使用这种方式!

 

 前面示例中的try/catch序列还可以更加精简。因为filenotfoundexception是ioexception的一个子类,所以不需要单独捕获。例如,这个catch语句可以用来捕获两个异常,从而不必单独捕获filenotfoundexception。这种情况下,将显示描述错误的标准异常信息。

方式四:

import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

public class test4 {
    public static void main(string[] args) {
        fileinputstream fis = null;
        byte[] buffer = new byte[1024];
        int temp = 0;
        
        try {
            //打开文件
            fis = new fileinputstream("e:/test_file/a.txt");
            
            //访问文件
            while(-1 != (temp = fis.read(buffer))) {
                system.out.print(new string(buffer, 0, temp));
            }
            
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            //关闭文件
            try {
                if(null != fis) {   //非空判断
                    fis.close();
                }
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }
}

在这种方法中,任何错误,包括打开文件时发生错误,都会被一个catch语句处理。这种方法十分简洁。但要注意,如果想单独处理打开文件时发生的错误(例如,用户错误地键入了文件名),这种方法就不合适了。

 

最后放一张图

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网