public static void main(String[] args) throws Exception {
// 1.7的新特性,小括號的代碼,在小括號後的大括號的代碼執行完之後,會自動調用close()方法.
// 因爲實現了AutoCloseable接口
try (FileInputStream fis = new FileInputStream("abc.txt");
FileOutputStream fos = new FileOutputStream("bcd.txt");) {
int i;
while ((i = fis.read()) != -1)
fos.write(i);
}
// 標準的try,finally處理
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream("bcd.txt");
fis = new FileInputStream("abc.txt");
int i;
while ((i = fis.read()) != -1)
fos.write(i);
} finally {
try {
if (fis != null) {
fis.close();
}
} finally {
if (fos != null)
fos.close();
}
}
}
}