-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTryWithResources.java
More file actions
62 lines (46 loc) · 1.94 KB
/
Copy pathTryWithResources.java
File metadata and controls
62 lines (46 loc) · 1.94 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package exceptions;
import java.io.*;
import java.nio.file.*;
import java.util.logging.*;
/*
* Try with resources, automatic closing of resources that implement
AutoCloseable without the need for a manual finally block.
*/
// Custom resource implementing AutoCloseable
class SimulatedConnection implements AutoCloseable {
private static final Logger connectionLogger = Logger.getLogger(SimulatedConnection.class.getName());
private final String name;
// Constructor to initialize the resource
SimulatedConnection(String name) {
this.name = name;
connectionLogger.log(Level.INFO, "Opening connection: {0}", name);
}
// Method to execute a query
void executeQuery() {
connectionLogger.log(Level.INFO, "{0} executing a query...", name);
}
// Method to close the connection
@Override
public void close() {
// This method is automatically called when exiting the try block
connectionLogger.log(Level.INFO, "Closing connection: {0}", name);
}
}
public class TryWithResources {
private static final Logger logger = Logger.getLogger(TryWithResources.class.getName());
public static void main() throws IOException {
// Example 1 | Custom resource
try (SimulatedConnection connection = new SimulatedConnection("Database")) {
connection.executeQuery();
}
// Here connection is already automatically closed even if an exception had occurred
// Example 2 | Writing to a file with automatic closing
Path tempFile = Path.of("temp_output.txt");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile.toFile()))) {
writer.write("Line written using try-with-resources.");
}
logger.log(Level.INFO, "File written successfully at: {0}", tempFile.toAbsolutePath());
// Clean up to avoid leaving residual files after execution
Files.deleteIfExists(tempFile);
}
}