}
public static void writeLines(String fileName, List<String> lines) throws IOException {
- BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8);
- for(String line : lines) {
- writer.write(line);
- writer.write("\n");
+ try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8)) {
+ for(String line : lines) {
+ writer.write(line);
+ writer.write("\n");
+ }
}
- writer.close();
}
public static void appendLine(String fileName, String line) throws IOException {
- BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8,
- StandardOpenOption.CREATE, StandardOpenOption.APPEND);
- writer.write(line);
- writer.write("\n");
- writer.close();
+ try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8,
+ StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
+ writer.write(line);
+ writer.write("\n");
+ }
}
}