在Java 7中尝试使用资源?

cit*_*onn 5 java-7 try-with-resources

在Java 7中新的Try-with-Resources语法中,我是否需要担心资源的顺序?

try (InputStream in = loadInput(...); // <--- can these be in any order?
     OutputStream out = createOutput(...) ){
    copy(in, out);
}
catch (Exception e) {
    // Problem reading and writing streams.
    // Or problem opening one of them.
    // If compound error closing streams occurs, it will be recorded on this exception 
    // as a "suppressedException".
} 
Run Code Online (Sandbox Code Playgroud)

Pro*_*cus 7

当且仅当使用正常的try {create resources} finally {close resources}语法时,顺序才有意义.首先获得的资源将最后关闭.有关详细信息,请参阅技术说明