java try() try{}

https://www.cnblogs.com/coder-zyc/p/10364528.html

  • JDK1.7之前

FileWriter fw = null;

try {

fw = new FileWriter("test\\test.txt");

fw.write("test");

} catch (Exception ex) {

ex.printStackTrace();

} finally {

if (fw != null) {

try {

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

  • JDK1.7之后

try(FileWriter fw = new FileWriter("test.txt")) {

fw.write("test");

} catch(Exception ex) {

ex.printStackTrace();

}

(0)

相关推荐