One, the introduction

You may have a lot of problems using streams because they are not closed, or they are not closed properly. In fact, java7 introduced the auto-close flow mechanism, we just didn’t use it.

Example of automatic closing flow

public class AutoCloseTest {

    public static void main(String[] args) throws IOException {

        // Place the open stream code in the try

        try (

                BufferedReader br = new BufferedReader(

                        new FileReader("teacher.txt"));

                PrintStream pr = new PrintStream(new FileOutputStream("a.txt"))

        ) {

            // Use two resources

            System.out.println(br.readLine());

            pr.println("I'm the man who's gonna be One Piece.");

        }

    }

}

Copy the code

We place the open stream object directly inside the parentheses of the try so that the stream is automatically closed when it is finished using it. This not only saves trouble but also avoids the error that may be brought by self-closing.

Three, the principle of inquiry

Is that amazing? Let us open this layer of mystery veil, see is where evil in this “trouble”. Using the JD-GUI tool, we open

Class file.

public class AutoCloseTest

{

  public static void main(String[] args)

    throws IOException

  
{

    BufferedReader br = new BufferedReader(new FileReader("teacher.txt")); Throwable localThrowable6 =null;

    try

    {

      PrintStream pr = new PrintStream(new FileOutputStream("a.txt")); Throwable localThrowable7 =null;

      try

      {

        System.out.println(br.readLine());

        pr.println("I'm the man who's gonna be One Piece.");

      }

      catch (Throwable localThrowable1)

      {

        localThrowable7 = localThrowable1;throw localThrowable1;

      }

      finally {}

    }

    catch (Throwable localThrowable4)

    {

      localThrowable6 = localThrowable4;throw localThrowable4;

    }

    finally

    {

      if(br ! =null) {

        if(localThrowable6 ! =null) {

          try

          {

            br.close();

          }

          catch (Throwable localThrowable5)

          {

            localThrowable6.addSuppressed(localThrowable5);

          }

        } else {

          br.close();

        }

      }

    }

  }

}

Copy the code

Awesome, JVM helps us to do better, you can refer to the previous written shutdown process has no problem. The same is true for the auto-close process, a stream that is closed before it is opened, because if it is closed before it is opened, and if it is opened after it is used before it is opened, this will throw an exception.

Why does it turn off automatically?

This is because java7 introduced an AutoCloseable interface. Almost everything Java native uses streams implements this interface. Here is the inheritance diagram.

Four,

By using the auto-close mechanism introduced in java7, we can open the exile implementing the AutoCloseable interface in the try block. This way, the flow will automatically close properly after use, thus eliminating the flow closure exception.