I have the following given code after some refactoring:
public static void storePreferences() {
try {
Preferences.userRoot().exportSubtree(new FileOutputStream(ReleaseInfo.getAppFolder() + File.separator + SETTINGSFILENAME));
} catch (IOException e) {
e.printStackTrace();
} catch (BackingStoreException e) {
e.printStackTrace();
}
}
Now I wonder - should I explicitly close the FileOutputStream
or the JVM takes care for doing so? I have looked at this question on the same topic, where it is said that it is not a good practice to rely on the garbage collector, but still I need a bit more confirmation (therefore this question). I have changed the above code to:
public static void storePreferences() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(ReleaseInfo.getAppFolder() + File.separator + SETTINGSFILENAME);
Preferences.userRoot().exportSubtree(fos);
} catch (IOException | BackingStoreException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here I close the stream manually, so which would be the better alternative and do the performed changes really bring something to the table?
Any other remarks?