1

I am trying to store my ArrayList between runtimes. So when I enter items into the ArrayList then close the program down and then open it back up and try to search the item I put in it will still be there.

I have tried MySQL and XML but I cannot figure it out. If you could please direct me in the direction of an easy way to store ArrayList that would be amazing.

Edit:

I am trying to serialize this ArrayList:

static ArrayList<Product> Chart=new ArrayList<Product>();

with these objects:

double Total;
String name;
double quantity;
String unit;
double ProductPrice;
oers
  • 18,436
  • 13
  • 66
  • 75
Vincent Taglia
  • 173
  • 1
  • 3
  • 12
  • possible duplicate of [Java MySQL integration with ArrayLists](http://stackoverflow.com/questions/11819212/java-mysql-integration-with-arraylists) – Jon Skeet Aug 06 '12 at 18:22
  • That thread seems to be more about basic database access, and very little is said about serialization there. I provided a very simple pair of methods here. – Andrew Mao Aug 06 '12 at 18:40

3 Answers3

4

Look into object serialization, it sounds like exactly what you want

What is object serialization?

This allows you to write a Java object to a file and read it back in later.

Community
  • 1
  • 1
Carl
  • 905
  • 5
  • 9
  • Note that the contents need to be serializable for this to work. – Adriaan Koster Aug 06 '12 at 19:50
  • What is an example of an object that is serializable and one that isnt? – Vincent Taglia Aug 06 '12 at 20:04
  • `Thread` for example is not serializable (that wouldn't make a lot of sense anyway). The basic thing is that all references in your object need to be to other Serializable objects as well, and most common data types are. http://java.sun.com/developer/technicalArticles/Programming/serialization/ – Carl Aug 06 '12 at 20:39
1

Here is an example. It also zips up the file, although you don't have to do that. You can also use the stream to write to a MYSQL blob instead of a file.

public static <T> void writeToGZIP(String filename, T obj) {
    ObjectOutputStream os = null;

    try {
        os = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(filename)));
        os.writeObject(obj);                    
        System.out.println("Object written to " + filename);
    } catch (Exception e) {         
        e.printStackTrace();            
    } finally {
        try {
            if (os != null) { os.flush(); os.close(); }
        } catch (IOException e) {           
            e.printStackTrace();
        }
    }       
}

@SuppressWarnings("unchecked")
public static <T> T readFromGZIP(String filename, T obj) {      
    ObjectInputStream ois = null;       

    try {
        ois = new ObjectInputStream(new GZIPInputStream(new FileInputStream(filename)));            
        obj = (T) ois.readObject();         
    } catch (Exception e) {         
        e.printStackTrace();            
    } finally { 
        try {
            if( ois != null ) ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return obj;
}
Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
0

ObjectOutputStream and ObjectInputStream

Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106