javaIO——AutoCloseable 小试

  IO outlined in the previously mentioned article too, AutoCloseable interface class will automatically call close () method, then what specifically how to write it? And under abnormal circumstances or more resources is not able to automatically call it? Let's write a simple class test will know.

 

  Overview slice Address: https://www.cnblogs.com/coding-one/p/11342024.html

 

  Test code:

package com.one.jdksrc.java.io;

import java.io.Closeable;
import java.io.IOException;

/**
* @author haoya_Coding_one
* @version 1.0
* @create 2019/8/13 9:32
* @description TODO
*/
public class CloseableTest {
public static void main(String[] args){

System.out.println("------------------在 try{} 中声明不会调用-----------------------");
try {
HowToAutoClose howToAutoClose = new HowToAutoClose("1");
howToAutoClose.doNothing();
}catch (Exception ex){
System.out.println(ex.getMessage());
}

System.out.println ( "------------------ the try () calls in a statement ----------------- ------ ");
the try (HowToAutoClose howToAutoClose new new HowToAutoClose = (". 1 ")) {
howToAutoClose.doNothing ();
} the catch (Exception EX) {
System.out.println (ex.getMessage ());
}

System.out.println ( "------------------ abnormal circumstances still be called, and is the first call to close and then catch the exception --------- -------------- ");
the try (howToAutoClose howToAutoClose new new howToAutoClose = (". 1 ")) {
howToAutoClose.doNothing ();
the throw a NullPointerException new new (" this is the abnormality information ");
} the catch (Exception EX) {
System.out.println (ex.getMessage ());
}

System.out.println ( "sequential ------------------ all resources will call, and declare the order of inverse ------------- ---------- ");
the try (HowToAutoClose howTo1 new new HowToAutoClose = (". 1 ");
HowToAutoClose howto2 new new HowToAutoClose = (" 2 ");
HowToAutoClose howTo3 new new HowToAutoClose = (". 3 ")) {
/ / invokes test whether a plurality of resources, and the call sequence
} the catch (Exception EX) {
System.out.println (ex.getMessage ());
}

System.out.println ( "--------- --------- try {} is also called the return ----------------------- ");
the try (HowToAutoClose howToAutoClose = new new HowToAutoClose ( ". 1")) {
howToAutoClose.doNothing ();
return;
} the catch (Exception EX) {
System.out.println(ex.getMessage());
}
}

public static class HowToAutoClose implements Closeable {
private String param = null;

public HowToAutoClose(String integer){
param = integer;
}

public void doNothing(){

}
@Override
public void close() throws IOException {
System.out.println(param);
}
}
}

  operation result:

 

  

 

  in conclusion:

    1. resource need to be declared in the try () parentheses;

    2. try () {} return or abnormality occurs in brace block, automatically calls still close () method;

    3. You can declare multiple resource instances, use ";" to separate and multiple resource instances will reverse call (in fact, as long as able to call, order is not important);

Guess you like

Origin www.cnblogs.com/coding-one/p/11368653.html