Use try-with-resources to close reader / writer

This commit is contained in:
Zhihong Yu 2023-06-30 14:56:03 -07:00 committed by Christian Beikov
parent de09122ba4
commit 402a89eb03
2 changed files with 11 additions and 15 deletions

View File

@ -32,8 +32,7 @@ public class SingleLineSqlScriptExtractor implements SqlScriptCommandExtractor {
public List<String> extractCommands(Reader reader, Dialect dialect) { public List<String> extractCommands(Reader reader, Dialect dialect) {
final List<String> statementList = new LinkedList<>(); final List<String> statementList = new LinkedList<>();
final BufferedReader bufferedReader = new BufferedReader( reader ); try (final BufferedReader bufferedReader = new BufferedReader( reader )) {
try {
for ( String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine() ) { for ( String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine() ) {
final String trimmedLine = line.trim(); final String trimmedLine = line.trim();

View File

@ -51,8 +51,8 @@ public class AntlrHelper {
tmpFile.getParentFile().mkdirs(); tmpFile.getParentFile().mkdirs();
tmpFile.createNewFile(); tmpFile.createNewFile();
final BufferedReader reader = new BufferedReader( new FileReader( generatedJavaFile ) ); try (final BufferedReader reader = new BufferedReader( new FileReader( generatedJavaFile ) );
final BufferedWriter writer = new BufferedWriter( new FileWriter( tmpFile ) ); final BufferedWriter writer = new BufferedWriter( new FileWriter( tmpFile ) )) {
boolean found = false; boolean found = false;
String currentLine; String currentLine;
@ -64,10 +64,7 @@ public class AntlrHelper {
} }
writer.write(currentLine + System.lineSeparator()); writer.write(currentLine + System.lineSeparator());
} }
}
writer.close();
reader.close();
generatedJavaFile.delete(); generatedJavaFile.delete();
tmpFile.renameTo( generatedJavaFile ); tmpFile.renameTo( generatedJavaFile );
} }