code: public static <T> T readObjectFromFile(String file, Class<T> clazz, boolean printException) {
          final FileInputStream fileIn;
          try {
              fileIn = new FileInputStream(file);
          } catch (Exception e) {
              printException(e, printException);
              return null;
          }
  
          final BufferedInputStream bufferedIn = new BufferedInputStream(fileIn);
          final ObjectInputStream objIn;
          try {
              objIn = new ObjectInputStream(bufferedIn);
          } catch (Exception e) {
              printException(e, printException);
              closeInputStream(fileIn, printException);
              return null;
          }
  
          final Object ret;
          try {
              ret = objIn.readObject();
          } catch (Exception e) {
              printException(e, printException);
              closeInputStream(objIn, printException); // objIn.close() also closes fileIn
              return null;
          }
  
          closeInputStream(objIn, printException); // objIn.close() also closes fileIn
          return clazz.cast(ret);
      }
docs: Reads an object of type {@code T} from {@code file}.
  
  @param file file from which the object should be read
  @param clazz non-null Class object for {@code T}
  @param printException whether or not any stacktraces should be printed
  @param <T> the return type
  @return possibly null object of type {@code T}.