Results 1 to 4 of 4
  1. #1
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Question Save/Load ArrayList

    I am busy making an MS-Paint-look-alike in Java. It uses JAplet. The applet is then shown in an application. In this way it can be run online as just the applet and as an application with IO options.

    My program excists of 25 classes. Several classes are tool classes (rect, fill rect, oval, fill oval). When the action MouseReleased is performed (when drawing a shape on the 'canvas') an object of the type 'SchetsElementen' is added to an ArrayList in the class 'Schets'. The following class is SchetsElementen
    Java Code:
    import java.awt.*;
    
    public class SchetsElementen
    {
    	Color c;
    	int iToolID, iLijnDikte; //LijnDikte means line width (stroke size).
    	Point p1, p2;
    	String s;
    	static final int 	PEN_TOOL = 			0,
    						LINE_TOOL = 		1,
    						RECT_TOOL = 		2, 
    						FILL_RECT_TOOL = 	3,
    						OVAL_TOOL = 		4,
    						FILL_OVAL_TOOL = 	5,
    						TEXT_TOOL = 		6;
    	
    	public SchetsElementen(Color kleur, int iTool, Point startpunt, Point eindpunt, String string, int iStroke)
    	{
    		p1 = startpunt;
    		p2 = eindpunt;
    		c = kleur;
    		iToolID = iTool;
    		s = string;
    		iLijnDikte = iStroke;
    	}
    	
    	public void teken(Graphics g)
    	{
    		Point     xy = minimumPunt(p1, p2);
            Dimension wh = puntAfstand(p1, p2);
    
    		g.setColor(c);
    		((Graphics2D) g).setStroke( new BasicStroke(iLijnDikte) );
    		switch (iToolID) 
    		{
    	        case PEN_TOOL: 										; break;
    	        case LINE_TOOL: 		g.drawLine(p1.x, p1.y, p2.x, p2.y)	; break;
    	        case RECT_TOOL: 		g.drawRect(xy.x, xy.y, wh.width, wh.height)	; break;
    	        case FILL_RECT_TOOL: 	g.fillRect(xy.x, xy.y, wh.width, wh.height)	; break;
    	        case OVAL_TOOL:  		g.drawOval(xy.x, xy.y, wh.width, wh.height)	; break;
    	        case FILL_OVAL_TOOL:  	g.fillOval(xy.x, xy.y, wh.width, wh.height)	; break;
    	        case TEXT_TOOL: 		g.drawString(s, xy.x, xy.y)		; break;
    		}
    	
    	}
    	
    	private static final long serialVersionUID = 1;
    }
    Now I want to be able to save the entire ArrayList to a file to be able to load it again. When loading the arraylist, the program will run through the ArrayList and redraw all elements in it. I just cant figure out how to save my ArrayList easily. I was thinking of Serialization, but I haven't yet grasped the concept. Could someone help me to make my ArrayList savable and loadable?

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    Without going deeper into problem
    here is basic ArrayList serialization and deserialization example for you:

    Java Code:
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    public class ArrayListSerial {
    
    	public static void main(String[] args) {
    
    
    		List<String> myList = new ArrayList<String>();
    		myList.add("1. buffon");
    		myList.add("10. del piero");
    		
    		/*serialize arraylist*/
    		try {
    			System.out.println("serializing list");
    			FileOutputStream fout = new FileOutputStream("list.dat");
    			ObjectOutputStream oos = new ObjectOutputStream(fout);
    			oos.writeObject(myList);
    			oos.close();
    		}
    		catch (Exception e) { 
    			e.printStackTrace(); 
    		}
    
    		
    		
    		/*unserialize arraylist */
    		
    		System.out.println("unserializing list");
    		try {
    			FileInputStream fin = new FileInputStream("list.dat");
    			ObjectInputStream ois = new ObjectInputStream(fin);
    			List<String> list = (ArrayList) ois.readObject();
    			
    			for (String s : list){
    				System.out.println(s);
    			}
    			
    			ois.close();
    		}
    		catch (Exception e) { 
    			e.printStackTrace(); 
    		}
    	
    
    
    
    	}//
    
    }//
    cheers!

  3. #3
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    Thnx a bunch. I fixed it in combination with a Jfilechooser. Now It does what I want it to do.

  4. #4
    Hallowed is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    Where does it put list.dat?
    ______________________
    Nevermind I found it.
    Can you specify where it saves it? like a url if need be?
    __________________________________________________ __
    and.. could the example be easily modified to work with an arraylist of Objects? not just Strings? Cause I tried and failed. >_>
    __________________________________________________ ___________
    Well I figured out that i needed to declare my class like this:
    public class Account implements Serializable

    It serializes correctly I think. At least it says it does..
    but I get this error when I try to load.

    unserializing list
    java.lang.ClassCastException: Account cannot be cast to java.lang.String
    at SerializeAccount.LoadArray(SerializeAccount.java:3 8)
    at Main.main(Main.java:15)
    line 38 starts here:
    Java Code:
    for (String s : list)
    			{
    				System.out.println(s);
    			}
    I'm guessing the problem is its trying to do something with the String, but it can't print strings since I'm serializing objects in my array list.
    ___________________________________-
    eh I took it out cause I figured that wasn't important for reading unserializing my list. But even though I do that, it doesn't remember my Accounts apparently.
    ___________________________
    Omg it worked! I changed: List<String> list = (ArrayList) ois.readObject();
    to: AAL.accArray = (ArrayList<Account>) ois.readObject();
    and it totally remembered my accounts! This is the best day of my life! =D
    Not really.. >_>'
    Anyway I'm done talking to myself.
    Thanks for that great example. =D
    It was of much help.
    Last edited by Hallowed; 05-08-2011 at 07:13 PM.

Similar Threads

  1. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 08:31 PM
  2. Arraylist Save and Load
    By frankycool in forum Advanced Java
    Replies: 1
    Last Post: 11-14-2009, 10:29 PM
  3. save will work but load wont?!?!
    By Sticks_ll in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:19 AM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. How to Save/Load Vector to/from file
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •