HashMap to file?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: HashMap to file?

  1. #1
    Join Date
    Nov 2006
    Posts
    4

    Exclamation HashMap to file?

    How can i write a HashMap to a file so it can be loaded the next time i run the program? i've been going crazy trying to figure this out!

    Thanks,
    ~X

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Use serialization

    Use serialization for any java object that you want to store in a file. Check out the usage of the ObjectOutput/Input -file.
    eschew obfuscation

  3. #3
    Join Date
    Nov 2006
    Posts
    4
    I already did that... it gives me this error:
    "java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: Player"

    here's my serialization class:

    Code:
    import java.util.*;
    import java.io.*;
    
    
    public class LoadSave extends Player implements Serializable 
    {
        private File filename = new File("sList.sav");
        private HashMap list;
          
        /**
         * 
         */
        public LoadSave(HashMap list)
        {
            this.list = list;
        }
    
        /**
         * 
         */
        public boolean load()
        {
            if(filename.exists())
                try {
                  ObjectInputStream in = new ObjectInputStream(
                  new FileInputStream(filename));
                  list = (HashMap)in.readObject();
                  in.close();
                }
                catch(ClassNotFoundException e) {
                    if (PlayXandersGame.DEBUG)
                        System.out.println(e);
                return false;
                } 
                catch (NotSerializableException e)  {
                    if (PlayXandersGame.DEBUG)
                        System.out.println(e);
                return false;
                }
                catch (FileNotFoundException e) {
                    if (PlayXandersGame.DEBUG)
                        System.out.println(e);
                return false;
                }
                catch(IOException e) {
                    if (PlayXandersGame.DEBUG)
                        System.out.println(e);
                return false;
                }
            return true;
        }
        public boolean save() 
        {
            try {
                System.out.println("Saving Character List");
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
                out.writeObject(list);
                out.close();
            } 
            catch (NotSerializableException e)  {
                if (PlayXandersGame.DEBUG)
                    System.out.println(e);
                return false;
            }
            catch (FileNotFoundException e) {
                if (PlayXandersGame.DEBUG)
                    System.out.println(e);
                return false;
            }
            catch(IOException e) {
                if (PlayXandersGame.DEBUG)
                    System.out.println(e);
                return false;
            }
            return true;
        }
    }
    Last edited by PaPa_Smerf; 11-14-2006 at 09:04 AM.

  4. #4
    Join Date
    Nov 2006
    Posts
    4
    Nevermind, i figured it out!

    Thx,
    ~X

Similar Threads

  1. Problem compiling a C++ file in Linux
    By kepler in forum C++
    Replies: 3
    Last Post: 08-26-2007, 01:11 AM
  2. Database not properly created
    By Arjuna in forum Database
    Replies: 2
    Last Post: 07-25-2007, 04:22 AM
  3. Replies: 6
    Last Post: 08-16-2006, 11:32 PM
  4. Either Overwrite or Rename file
    By Shaitan00 in forum Java
    Replies: 1
    Last Post: 04-06-2005, 07:04 AM
  5. Test a file to see if it's open
    By JohnN in forum VB Classic
    Replies: 12
    Last Post: 05-08-2001, 05:25 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center