Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use try with resources when writing out json files #93

Merged
merged 1 commit into from
Mar 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions src/com/bolsinga/web/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,41 +623,17 @@ public static JSONArray createJSONArray(final String sourceFile) throws com.bols
}

public static void writeJSON(final JSONObject json, final String outputFile) throws com.bolsinga.web.WebException {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outputFile);
try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(outputFile), java.nio.charset.Charset.forName("UTF-8"))) {
if (com.bolsinga.web.Util.getPrettyOutput()) {
osw.write(json.toString(2));
} else {
json.write(osw);
}
} catch (IOException e) {
StringBuilder sb = new StringBuilder();
sb.append("Can't find file: ");
sb.append(outputFile);
throw new com.bolsinga.web.WebException(sb.toString(), e);
}

OutputStreamWriter osw = new OutputStreamWriter(fos, java.nio.charset.Charset.forName("UTF-8"));
try {
try {
if (com.bolsinga.web.Util.getPrettyOutput()) {
osw.write(json.toString(2));
} else {
json.write(osw);
}
} catch (Exception e) {
StringBuilder sb = new StringBuilder();
sb.append("Can't write file: ");
sb.append(outputFile);
throw new com.bolsinga.web.WebException(sb.toString(), e);
}
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
StringBuilder sb = new StringBuilder();
sb.append("Unable to close: ");
sb.append(outputFile);
throw new com.bolsinga.web.WebException(sb.toString(), e);
}
}
}
}
}