Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5987029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:45:22+00:00 2026-05-22T22:45:22+00:00

I have an object caching class that can save an object in memory, the

  • 0

I have an object caching class that can save an object in memory, the problem arises when I try to retreive it later. How do I output a generic Object and then put it into a defined object on the other end. Here is the class

public class ObjectCache implements Serializable{

private static final long serialVersionUID = 1L;
private Context context;
private String objectName;
private Integer keepAlive = 86400;

public ObjectCache(Context context,String objectName) {
    this.context = context;
    this.objectName = objectName;
}

public void setKeepAlive(Integer time) {
    this.keepAlive = time;
}

public boolean saveObject(Object obj) {

    final File cacheDir = context.getCacheDir();
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;

    if (!cacheDir.exists()) {
        cacheDir.mkdirs();
    }

    final File cacheFile = new File(cacheDir, objectName);

    try {

        fos = new FileOutputStream(cacheFile);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
            if (fos != null) {
                fos.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}

public boolean deleteObject() {

    final File cacheDir = context.getCacheDir();
    final File cacheFile = new File(cacheDir, objectName);
    return (cacheFile.delete());
}

public Object getObject() {

    final File cacheDir = context.getCacheDir();
    final File cacheFile = new File(cacheDir, objectName);

    Object simpleClass = null;
    FileInputStream fis = null;
    ObjectInputStream is = null;

    try {

        fis = new FileInputStream(cacheFile);
        is = new ObjectInputStream(fis);
        simpleClass = (Object) is.readObject();
    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (is != null) {
                is.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return simpleClass;

}
}

And from activity I start the object class, save it, and retreive it like so

objectCache = new ObjectCache(this,"sessionCache2");
//save object returns true
boolean result = objectCache.saveObject(s);

//get object back
Object o = objectCache.getObject();

instead of Object o i need it to be a Session object, but then that will mean the return type of the getObject method needs to return that. Can’t I convert the object some

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:45:23+00:00Added an answer on May 22, 2026 at 10:45 pm

    If you are sure that the object that the ObjectCache will return is a Session object all you have to do is cast it:

    //get object back
    Session session = (Session) objectCache.getObject();
    

    This will throw a ClassCastException (unchecked) if the getObject() method does not return a Session object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object of class F. I want to output the contents of
I have an object that is mapped to a cookie as a serialized base-64
I have an object in a multi-threaded environment that maintains a collection of information,
I have an object that implements IDisposable that is registered with the Windsor Container
I have an object that needs a test if the object data is valid.
I have an object, that is facing a particular direction with (for instance) a
I have a base class that declares a private non-static reference to the DataBase
I'm making a custom ListCellRenderer. I know that you can have different dimensions for
So I have a class which overrides Equals(object obj) and GetHashCode() along with implementing
Problem: When caching an instance of a class and immediately getting it back out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.