Java sonraki sürümlerinde
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(...)
...
}
catch (IOException e) {
throw new RuntimeException(...)
}
finally {
// or use the Apache Commons IOUtils.closeQuietly(outputStream);
// and then only need the one line
if (outputStream != null) {
try {
outputStream.close();
}
catch (Exception ignore) { }
}
}
, sen kullanabilirsiniz. Örnek:
public static void store(Properties properties, Class script) throws IOException {
ScriptManifest scriptManifest = (ScriptManifest) script.getAnnotation(ScriptManifest.class);
if (scriptManifest != null) {
String name = scriptManifest.name();
try (FileOutputStream outputStream = new FileOutputStream(Constants.SCRIPT_PROPERTIES_DIR + File.separator + name + ".properties")) {
properties.store(outputStream, "");
}
} else {
throw new RuntimeException("Script " + script.getName() + " does not have a ScriptManifest.");
}
}
veya nihayet böyle engellemek deneyin:
public static void store(Properties properties, Class script) throws IOException {
ScriptManifest scriptManifest = (ScriptManifest) script.getAnnotation(ScriptManifest.class);
if (scriptManifest != null) {
String name = scriptManifest.name();
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(Constants.SCRIPT_PROPERTIES_DIR + File.separator + name + ".properties");
properties.store(outputStream, "");
} finally {
if (outputStream != null) outputStream.close();
}
} else {
throw new RuntimeException("Script " + script.getName() + " does not have a ScriptManifest.");
}
}
@OliverCharlesworth Üzgünüm, OutputStream ment. – user2997204
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html –