1
0
forked from phoedos/pmd

use try with resource

This commit is contained in:
XenoAmess
2020-08-24 16:50:41 +08:00
parent 3a7ea4b606
commit c5e2234763
2 changed files with 3 additions and 15 deletions
pmd-core/src/test/java/net/sourceforge/pmd/cpd
pmd-dist/src/test/java/net/sourceforge/pmd/it

@ -54,9 +54,7 @@ public class FileReporterTest {
}
private String readFile(File file) throws IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
StringBuffer buffer = new StringBuffer();
String line = reader.readLine();
while (line != null) {
@ -67,10 +65,6 @@ public class FileReporterTest {
}
}
return buffer.toString();
} finally {
if (reader != null) {
reader.close();
}
}
}

@ -39,8 +39,7 @@ public class ZipFileExtractor {
* @throws Exception if any error happens during extraction
*/
public static void extractZipFile(Path zipPath, Path tempDir) throws Exception {
ZipFile zip = new ZipFile(zipPath.toFile());
try {
try (ZipFile zip = new ZipFile(zipPath.toFile())) {
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
@ -57,8 +56,6 @@ public class ZipFileExtractor {
}
}
}
} finally {
zip.close();
}
}
@ -70,15 +67,12 @@ public class ZipFileExtractor {
*/
public static List<String> readZipFile(Path zipPath) throws Exception {
List<String> result = new ArrayList<>();
ZipFile zip = new ZipFile(zipPath.toFile());
try {
try (ZipFile zip = new ZipFile(zipPath.toFile())) {
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
result.add(entry.getName());
}
} finally {
zip.close();
}
return result;
}