在Java中,try-with-resources语句是一个try语句,用于声明一个或多个资源。 该资源是一个对象,在完成程序后必须将其关闭。 try-with-resources语句可确保在语句执行结束时关闭每个资源。您可以传递实现java.lang.AutoCloseable的任何对象,其中包括实现java.io.Closeable的所有对象。 一个字符串到文件中。 它使用FileOutputStream的实例将数据写入文件。 FileOutputStream是一种资源,程序完成后必须将其关闭。 因此,在此示例中,资源关闭是由try本身完成的。
Try-with-resources 示例 1
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.io.FileOutputStream; public class TryWithResources { public static void main(String args[]){ try(FileOutputStream fileOutputStream = new FileOutputStream("/java7-new-features/src/abc.txt")){ String msg = "hello world!"; byte byteArray[] = msg.getBytes(); fileOutputStream.write(byteArray); System.out.println("Message written to file successfully!"); }catch(Exception exception){ System.out.println(exception); } } } |
Try-with-resources 示例 : 使用多个 Resources
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; public class TryWithResources { public static void main(String args[]){ try( FileOutputStream fileOutputStream =new FileOutputStream("/java7-new-features/src/abc.txt"); InputStream input = new FileInputStream("/java7-new-features/src/abc.txt")){ // ---------Code to write data into file----------// String msg = "hello world!"; byte byteArray[] = msg.getBytes(); fileOutputStream.write(byteArray); System.out.println("------------Data written into file--------------"); System.out.println(msg); // ---------Code to read data from file--------------// DataInputStream inst = new DataInputStream(input); int data = input.available(); byte[] byteArray2 = new byte[data]; inst.read(byteArray2); String str = new String(byteArray2); System.out.println("------------Data read from file--------------"); System.out.println(str); }catch(Exception exception){ System.out.println(exception); } } } |
您可以将try和finally块与try-with-resources语句一起使用,就像普通的try语句一样。
注:在try-with-resources语句中,catch或finally块在声明的资源关闭后执行。
Try-with-resources 示例: 使用 finally 语句块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.io.FileOutputStream; public class TryWithResources { public static void main(String args[]){ try( FileOutputStream fileOutputStream= new FileOutputStream("/home/irfan/scala-workspace/java7-new-features/src/abc.txt")){ // ----------Code to write data into file--------------// String msg = "hello world!"; byte byteArray[] = msg.getBytes(); fileOutputStream.write(byteArray); System.out.println("Data written successfully!"); }catch(Exception exception){ System.out.println(exception); }finally{ System.out.println("在关闭资源后执行."); } } } |
抑制的异常
在使用try-with-resources语句的时候,异常可能发生在try语句中,也可能发生在释放资源时。如果资源初始化时或try语句中出现异常,而释放资源的操作正常执行,try语句中的异常会被抛出;如果try语句和释放资源都出现了异常,那么最终抛出的异常是try语句中出现的异常,在释放资源时出现的异常会作为被抑制的异常添加进去,即通过Throwable.addSuppressed方法来实现。Java在Throwable类中添加了新的构造函数和两个新方法 处理被抑制的异常。
1 |
protected Throwable(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) |
它使用指定的详细消息,原因,启用或禁用抑制以及可写堆栈跟踪启用或禁用来构造新的throwable。
1 |
public final void addSuppressed(Throwable exception) |
它将指定的异常附加到为了传递此异常而被抑制的异常。 此方法是线程安全的,通常由try-with-resources语句(自动和隐式)调用。 它引发以下异常:IllegalArgumentException:如果异常是可抛出的,则throwable无法抑制自身。 NullPointerException:如果exception为null。
1 |
public final Throwable[] getSuppressed() |
它返回一个数组,其中包含所有由try-with-resources语句抑制的异常。 如果没有抑制异常或禁用抑制,则返回一个空数组。