|
1 | 1 | package core;
|
2 | 2 |
|
3 | 3 | import java.io.File;
|
| 4 | +import java.io.FileOutputStream; |
4 | 5 | import java.io.FileWriter;
|
5 | 6 | import java.io.IOException;
|
6 | 7 | import java.io.InputStream;
|
| 8 | +import java.io.OutputStreamWriter; |
7 | 9 | import java.nio.charset.StandardCharsets;
|
8 | 10 | import java.nio.file.DirectoryStream;
|
9 | 11 | import java.nio.file.Files;
|
@@ -79,10 +81,14 @@ public static Set<Path> listFiles(Path dir) throws IOException {
|
79 | 81 | * If the directory path is invalid or unable to be located.
|
80 | 82 | */
|
81 | 83 | public static Set<Path> listFiles(Path dir, FileExtension extension) throws IOException {
|
| 84 | + if (dir == null) { |
| 85 | + throw new IllegalArgumentException("Path cannot be null"); |
| 86 | + } |
82 | 87 | Set<Path> pathSet = new HashSet<>();
|
83 | 88 | dir = dir.normalize();
|
84 |
| - if (!Files.exists(dir)) |
| 89 | + if (!Files.exists(dir)) { |
85 | 90 | throw new IOException("The specified path, " + dir.toString() + ", was not found.");
|
| 91 | + } |
86 | 92 | try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
|
87 | 93 | for (Path path : stream) {
|
88 | 94 | String fileName = path.getFileName().toString();
|
@@ -140,8 +146,10 @@ public static void writeFile(String filename, String extension, Path destination
|
140 | 146 | public static void writeFile(File file, List<String> content) {
|
141 | 147 | try {
|
142 | 148 | Files.createDirectories(file.getParentFile().toPath());
|
143 |
| - file.createNewFile(); |
144 |
| - try (FileWriter writer = new FileWriter(file, false)) { |
| 149 | + if (!file.createNewFile() && !file.exists()) { |
| 150 | + throw new IOException("Failed to create new file: " + file.getAbsolutePath()); |
| 151 | + } |
| 152 | + try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, false), StandardCharsets.UTF_8)) { |
145 | 153 | for (String line : content) {
|
146 | 154 | writer.write(line + "\n");
|
147 | 155 | }
|
|
0 commit comments