jdk新特性

jdk 7

try 语法使用

try后面跟()括号,用来管理释放资源,try括号内的资源会在try语句结束后自动释放,前提是这些可关闭资源必须实现 java.lang.AutoCloseable接口

templet

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
package com.download;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class WordCount {
public static void main(String[] args) {
try (
InputStream in = new FileInputStream("");
OutputStream out = new FileOutputStream("");
)
{
byte[] buf = new byte[8192];
int i;
while ((i = in.read(buf)) != -1) {
out.write(buf, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}