Try With Resources Suppressed Exceptions
Auto-close resources with try-with-resources and handle suppressed exceptions.
Code
import java.io.*;
import java.nio.file.*;
public class FileProcessor {
public String readAndCopy(String src, String dst) throws IOException {
try (var reader = Files.newBufferedReader(Path.of(src));
var writer = Files.newBufferedWriter(Path.of(dst))) {
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
return "done";
}
}
}Report a problem with this exampleOpens a pre-filled issue on GitHub.