Close the stream safely

This commit is contained in:
libraryaddict 2014-04-05 03:47:33 +13:00
parent 9a4a9b9845
commit 2879834a7e

View File

@ -41,17 +41,25 @@ public class LibsDisguises extends JavaPlugin {
saveDefaultConfig(); saveDefaultConfig();
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml")); FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml"));
boolean needToSaveConfig = false; boolean needToSaveConfig = false;
InputStream stream = null;
try { try {
InputStream stream = getClassLoader().getResource("config.yml").openStream(); stream = getClassLoader().getResource("config.yml").openStream();
for (String option : YamlConfiguration.loadConfiguration(stream).getKeys(false)) { for (String option : YamlConfiguration.loadConfiguration(stream).getKeys(false)) {
if (!config.contains(option)) { if (!config.contains(option)) {
config.set(option, getConfig().get(option)); config.set(option, getConfig().get(option));
needToSaveConfig = true; needToSaveConfig = true;
} }
} }
stream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
if (needToSaveConfig) { if (needToSaveConfig) {
try { try {