io学习二

io异常的处理##

old style

private static void printFile() throws IOException {
    InputStream input = null;

    try {
        input = new FileInputStream("file.txt");

        int data = input.read();
        while(data != -1){
            System.out.print((char) data);
            data = input.read();
        }
    } finally {
        if(input != null){
            input.close();
        }
    }
}

这个代码冗长 并且 如果最后close失败 最终会抛出final块中的异常,无论try捕获的异常是否更有 意义。

JDK7 try-with-resouces

private static void printFileJava7() throws IOException {

    try(FileInputStream input = new FileInputStream("file.txt")) {

        int data = input.read();
        while(data != -1){
            System.out.print((char) data);
            data = input.read();
        }
    }
}

This is the try-with-resources construct. The FileInputStream variable is declared inside the parentheses after the try keyword. Additionally, a FileInputStream is instantiated and assigned to the variable.

When the try block finishes the FileInputStream will be closed automatically. This is possible because FileInputStream implements the Java interface java.lang.AutoCloseable. All classes implementing this interface can be used inside the try-with-resources construct.

If an exception is thrown both from inside the try-with-resources block, and when the FileInputStream is closed (when close() is called), the exception thrown inside the try block is thrown to the outside world. The exception thrown when the FileInputStream was closed is suppressed. This is opposite of what happens in the example first in this text, using the old style exception handling (closing the resources in the finally block).
jdk7 新语法中 使用try-with-resouces块之后 关闭资源更简单,所有实现了 AutoClose接口的都可以在try块之后被关闭,并且当发生异常之后抛出的是try块中捕获的异常。

Using Multiple Resources

private static void printFileJava7() throws IOException {

    try(  FileInputStream     input         = new FileInputStream("file.txt");
          BufferedInputStream bufferedInput = new BufferedInputStream(input)
    ) {

        int data = bufferedInput.read();
        while(data != -1){
            System.out.print((char) data);
    data = bufferedInput.read();
        }
    }
}

This example creates two resources inside the parentheses after the try keyword. An FileInputStream and a BufferedInputStream. Both of these resources will be closed automatically when execution leaves the try block.

The resources will be closed in reverse order of the order in which they are created / listed inside the parentheses. First the BufferedInputStream will be closed, then the FileInputStream.
多个资源声明也是可以被关闭的,关闭的顺序是 声明的顺序相反的。

Custom AutoClosable Implementations

The try-with-resources construct does not just work with Java's built-in classes. You can also implement the java.lang.AutoCloseable interface in your own classes, and use them with the try-with-resources construct.

The AutoClosable interface only has a single method called close(). Here is how the interface looks:

public interface AutoClosable {

    public void close() throws Exception;
}

Any class that implements this interface can be used with the try-with-resources construct. Here is a simple example implementation:

public class MyAutoClosable implements AutoCloseable {

    public void doIt() {
        System.out.println("MyAutoClosable doing it!");
    }

    @Override
    public void close() throws Exception {
        System.out.println("MyAutoClosable closed!");
    }
}

The doIt() method is not part of the AutoClosable interface. It is there because we want to be able to do something more than just closing the object.

Here is an example of how the MyAutoClosable is used with the try-with-resources construct:

private static void myAutoClosable() throws Exception {

    try(MyAutoClosable myAutoClosable = new MyAutoClosable()){
        myAutoClosable.doIt();
    }
}

Here is the output printed to System.out when the method myAutoClosable() is called:

MyAutoClosable doing it!
MyAutoClosable closed!

As you can see, try-with-resources is a quite powerful way of making sure that resources used inside a try-catch block are closed correctly, no matter if these resources are your own creation, or Java's built-in components.

IO异常处理模板

http://tutorials.jenkov.com/java-exception-handling/exception-handling-templates.html

原文地址:https://www.cnblogs.com/joeCqupt/p/6878361.html