Results 1 to 3 of 3
  1. #1
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default [SOLVED] Saving Several arraylists into one file?

    Hi again! :)

    I'm looking to save several arrayLists, and then being able to load them again. Currently I used a save function which saves the arraylists in seperate files, which is "user chosen name"+arrayListName. However I am unable to find a way to conveniently load these for the user, as he'd have to load each one separately.

    Thus I am looking for a way to either load these in a simple way, or beeing able to write several arrayLists into a single file, so the user only will need to choose one file. Or a way to load several files while the user will only need to choose one?

    I've tried googling for different variants of "several arraylists java" or looking through sites with how to use ArrayLists, but so far it seems as if it impossible.

    Is this even possible? And if so, any pointers to how? :)

    Like, can I make an ArrayList containing array lists, and then load the 4 different arraylists from there?

    Any tips are welcome!
    Last edited by Cemi; 04-26-2010 at 02:05 PM.
    Carpe Diem
    Each day's a gift and not a given right

  2. #2
    OcketN is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Hello Cemi!

    I was just wondering what type of data you intend on placing in the arrayLists? Because if it is not String or some sort of numeral type, i wonder how you are planning on mapping the objects in a file.

    could you also give a bit more information on the problem you are trying to solve with the arrayLists. Why are trying to save them to a file? Perhaps java already knows a solution to your problem!

    Nothing is impossible in Java!

    What i would do i create your very own loader to load saved arrayLists. For every person/user I would create only one file, and save every arrayList in that file. Since it is your application, you can control the layout of that file. You can separate the elements from an arrayList in your file by ';'. And every saved arraylist in your file is separated by a ------ line.

    Then you can write your own loader that lets the user select a file to read. Using a StringTokenizer you can separate a read line into tokens. You can say to the tokenizer that he needs to cut the string up on the places where he finds a ';'. If your loader encounters a '------' line, he knows that one arraylist has finished, so he can cache that arraylist already, start filling another.

    When the file is read, you give the user a choice of the loaded arraylists.

    You will need your own writer as well, where you let the user make a file using his name. Between every element of the arraylist you put a ';'. After every arrayList you write ofc the '-----' line.

    With the information i have now, that is what i would do :)

    Good luck!

  3. #3
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default

    I actually managed to fix it now, thanks however :)

    I was saving one arraylist of Strings, two with Color and one with Shapes. I did however just make an ArrayList<ArrayList> and added them. When I then loaded them I just set my old arraylists to equal the ones saved in the file.

    The relevant parts of my code.

    Java Code:
    private static List<ArrayList> listene = new ArrayList<ArrayList>();
      private static List<Shape> shapes = new ArrayList<Shape>();
      private static List<String> fyll = new ArrayList<String>();
      private static List<Color> border = new ArrayList<Color>();
      private static List<Color> fFill = new ArrayList<Color>();
    
    ActionListener saveModelListener = new ActionListener() {
        	        public void actionPerformed(ActionEvent e) {
        	        	listene.clear();
        	        	listene.add((ArrayList) shapes);
        	        	listene.add((ArrayList) fyll);
        	        	listene.add((ArrayList) border);
        	        	listene.add((ArrayList) fFill);
        	        	JFileChooser chooser = new JFileChooser(".");
        	    		int status = 
        	          chooser.showSaveDialog(frame);
        	    		if (status == JFileChooser.APPROVE_OPTION) {
        	    			File file = chooser.getSelectedFile();
        	    			try {
        	    				FileOutputStream out = new FileOutputStream( file );
        	    				ObjectOutputStream objectOut = new ObjectOutputStream( out );
        	    				objectOut.writeObject( listene );
        	    			} catch (Exception x) {
        	    				System.err.println("Try again: " + x);
        	    			}
        	    		}
        	        }
        	    };
        	    
        	    ActionListener openModelListener = new ActionListener() {
        	        public void actionPerformed(ActionEvent e) {
        	        	JFileChooser loadchooser = new JFileChooser(".");
        	          	File lastetFil;
        	              int status = 
        	              loadchooser.showOpenDialog(frame);
        	              if (status == JFileChooser.APPROVE_OPTION) {
        	               	File loadfile = loadchooser.getSelectedFile();
        	               	try {
        	               		ObjectInputStream fileIn = new ObjectInputStream(new FileInputStream(loadfile));
        	               		listene = (ArrayList) fileIn.readObject();
        	               		shapes = listene.get(0);
        	               		fyll = listene.get(1);
        	               		border = listene.get(2);
        	               		fFill = listene.get(3);
        	               		frame.repaint();
        	               		fileIn.close();
        	               	}
        	               	catch (Exception x) {
        	               		System.err.println("Try again: " + x);
        	                  }
        	              }
        	        }
        	    };
    Carpe Diem
    Each day's a gift and not a given right

Similar Threads

  1. read and write arraylists to a file
    By corney_16 in forum New To Java
    Replies: 2
    Last Post: 04-20-2010, 11:50 AM
  2. Replies: 8
    Last Post: 02-18-2010, 12:54 PM
  3. Problem Saving a file
    By GraemeH in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 12:14 AM
  4. Saving to a Text File
    By jadaleus in forum Advanced Java
    Replies: 2
    Last Post: 10-17-2008, 06:50 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
  •