-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
29 lines (25 loc) · 914 Bytes
/
Copy pathMain.java
File metadata and controls
29 lines (25 loc) · 914 Bytes
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
29
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws Exception {
tryBufferedReader();
tryCustomResource();
}
private static void tryBufferedReader() throws IOException {
try (BufferedReader br =
new BufferedReader(
new FileReader(
System.getProperty("user.dir") + "/java7/try-with-resources/src/notes.txt")); ) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} // No need of closing br in a finally block, since it is done automatically
}
private static void tryCustomResource() throws Exception {
try (CustomResource resource = new CustomResource(); ) {
resource.readCustomResource();
} // No need to do a resource.close() here since it will be called automatically
}
}