Results 1 to 4 of 4
Thread: Save/Load ArrayList
- 03-15-2010, 02:39 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
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; }
- 03-16-2010, 02:47 AM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 366
- Rep Power
- 12
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(); } }// }//
- 03-16-2010, 08:58 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
Thnx a bunch. I fixed it in combination with a Jfilechooser. Now It does what I want it to do.
- 05-08-2011, 07:12 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
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); }
___________________________________-
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 08:13 PM.
Similar Threads
-
How can i save the data Internally(auto save)
By Rama Koti Reddy in forum AWT / SwingReplies: 2Last Post: 11-01-2010, 09:31 PM -
Arraylist Save and Load
By frankycool in forum Advanced JavaReplies: 1Last Post: 11-14-2009, 11:29 PM -
save will work but load wont?!?!
By Sticks_ll in forum New To JavaReplies: 1Last Post: 06-12-2008, 05:19 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 12:43 PM -
How to Save/Load Vector to/from file
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 09:37 PM
Bookmarks