Use try-with-resources.

This commit is contained in:
Isira Seneviratne 2020-11-06 06:16:13 +05:30
parent 8bcf0c6498
commit 95333d37c8
8 changed files with 84 additions and 137 deletions

View file

@ -80,24 +80,15 @@ public class Utility {
@SuppressWarnings("unchecked")
public static <T> T readFromFile(File file) {
T object;
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(file));
try (ObjectInputStream objectInputStream =
new ObjectInputStream(new FileInputStream(file))) {
object = (T) objectInputStream.readObject();
} catch (Exception e) {
Log.e("Utility", "Failed to deserialize the object", e);
object = null;
}
if (objectInputStream != null) {
try {
objectInputStream.close();
} catch (Exception e) {
//nothing to do
}
}
return object;
}