Charset charset = Charset.forName("US-ASCII");
try (BufferedReader reader = Files.newBufferedReader(file, charset)) {
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
I saw such a piece of code on The Java Tutorials, in which a bracket statement is after key word 'try'. Is this valid? why cannot eclipse recognize it and report syntax error?
解决方案
It is the Java 7's try with resource feature.
You need to set Eclipse's Compiler Compliance level to 1.7 for it to recognize the syntax.
You can either set it globally under Window > Preferences > Java > Compiler or simply right-click on the project and do as shown below: