Skip to content

Commit 94eae7f

Browse files
committed
Do some light reformatting
1 parent 2996443 commit 94eae7f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/main/java/core/FileOperations.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package core;
22

33
import java.io.File;
4+
import java.io.FileOutputStream;
45
import java.io.FileWriter;
56
import java.io.IOException;
67
import java.io.InputStream;
8+
import java.io.OutputStreamWriter;
79
import java.nio.charset.StandardCharsets;
810
import java.nio.file.DirectoryStream;
911
import java.nio.file.Files;
@@ -79,10 +81,14 @@ public static Set<Path> listFiles(Path dir) throws IOException {
7981
* If the directory path is invalid or unable to be located.
8082
*/
8183
public static Set<Path> listFiles(Path dir, FileExtension extension) throws IOException {
84+
if (dir == null) {
85+
throw new IllegalArgumentException("Path cannot be null");
86+
}
8287
Set<Path> pathSet = new HashSet<>();
8388
dir = dir.normalize();
84-
if (!Files.exists(dir))
89+
if (!Files.exists(dir)) {
8590
throw new IOException("The specified path, " + dir.toString() + ", was not found.");
91+
}
8692
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
8793
for (Path path : stream) {
8894
String fileName = path.getFileName().toString();
@@ -140,8 +146,10 @@ public static void writeFile(String filename, String extension, Path destination
140146
public static void writeFile(File file, List<String> content) {
141147
try {
142148
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)) {
145153
for (String line : content) {
146154
writer.write(line + "\n");
147155
}

src/main/java/core/JSONObject.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ public Iterator<Object> iterator() {
305305
*
306306
* @see JSONObject#toString(int)
307307
*/
308+
@Override
308309
public String toString() {
309310
return toString(0);
310311
}
@@ -349,7 +350,12 @@ else if (value instanceof List<?>) {
349350
if (item == null) {
350351
System.out.println("null");
351352
}
352-
sb.append(item.toString(indent + 1));
353+
if (item != null) {
354+
sb.append(item.toString(indent + 1));
355+
}
356+
else {
357+
sb.append("null");
358+
}
353359
if (i < list.size() - 1)
354360
sb.append(",");
355361
sb.append("\n");
@@ -450,6 +456,11 @@ else if (o instanceof List<?>)
450456
return entries;
451457
}
452458

459+
@Override
460+
public int hashCode() {
461+
return this.toString().hashCode();
462+
}
463+
453464
/**
454465
* Determine whether this JSONObect is equal to the other JSONObject by comparing their String representations.
455466
*

0 commit comments

Comments
 (0)