11 Replies - 589 Views - Last Post: 11 July 2010 - 09:15 PM Rate Topic: -----

#1 harmy01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 179
  • Joined: 15-June 10

Object Serialization

Posted 11 July 2010 - 07:26 PM

Hello guys,

Little help here, i'am trying to save objects in a ArrayList but getting weird problems.

Following method saving the ArrayList by serialization

 public void saveData(){

        try{
            ObjectOutputStream saveFile  = new ObjectOutputStream(new FileOutputStream("SaveObjects.ser"));
            saveFile.writeObject(lstAccount);
            saveFile.flush();
            saveFile.close();
        }catch (IOException e){

        }
    }


And this method reads the saved file

public ArrayList<Account> readSavedObject(){
        ArrayList<Account> lstOfAccount = new ArrayList<Account>();
        try{

        ObjectInputStream readFile = new ObjectInputStream(new FileInputStream("SaveObjects"));
        lstOfAccount = (ArrayList) readFile.readObject();
        readFile.close();

    }catch (Exception e){

    }
        return lstOfAccount;
    }



i'am, getting a problem in the following method. The ArrayList which being saving contains 1 account Name hs password: passme. but however its not finding that Account for some reason. This is my first time use serialization. so help would be much appreciated.

 public void FindAccount() {
        ArrayList<Account> tempAccountlst = new ArrayList<Account>();

        //reading from file saveObject.ser
        tempAccountlst = readSavedObject();

        try {
            String passW = new String(password.getPassword());
            for (Account tempUser : tempAccountlst) {

                if (tempUser.getName().equals(user.getText())
                        && tempUser.getPassword().equals(passW)) {
                    adminFrame.setVisible(true);
                     setVisible(false);
                }else{
                     System.out.println("test");
                }
            }
        } catch (Exception e) {
            System.out.println("lssssssss");
        }
    }

This post has been edited by harmy01: 11 July 2010 - 07:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Object Serialization

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2860
  • View blog
  • Posts: 10,964
  • Joined: 15-July 08

Re: Object Serialization

Posted 11 July 2010 - 07:30 PM

ObjectInputStream readFile = new ObjectInputStream(new FileInputStream("SaveObjects"));



Didn't you call the file SaveObjects.ser? not just SaveObjects?
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Object Serialization

Posted 11 July 2010 - 07:37 PM

View PostDogstopper, on 11 July 2010 - 08:30 PM, said:

ObjectInputStream readFile = new ObjectInputStream(new FileInputStream("SaveObjects"));



Didn't you call the file SaveObjects.ser? not just SaveObjects?

:^:

@harmy01 does your Account class implements Serializable
Was This Post Helpful? 0
  • +
  • -

#4 harmy01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 179
  • Joined: 15-June 10

Re: Object Serialization

Posted 11 July 2010 - 07:51 PM

Thanks lol and + my Account class did not implement Serializable so they cound't be saved into ArrayList


btw, any idea where is the saveObject.ser is saved? im using netbeans
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Object Serialization

Posted 11 July 2010 - 07:54 PM

Should be in the same directory than your .java file unless you use package
in that case will be in the root directory of your package directory
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2860
  • View blog
  • Posts: 10,964
  • Joined: 15-July 08

Re: Object Serialization

Posted 11 July 2010 - 08:01 PM

If you actually run it via netbeans, it actually is in the project directory (not dist, src, build, ...), meaning it's in the same directory that contains those.
Was This Post Helpful? 0
  • +
  • -

#7 harmy01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 179
  • Joined: 15-June 10

Re: Object Serialization

Posted 11 July 2010 - 08:46 PM

1 more question plz (sorry for asking to many question)

Object Serialization does it auto save an object ? for instance, soon as i update an object inside the ArrayList.

i'am using the following code To change the password of The userAccount.

}else if(event.getSource() == (JButton) userPropertiesJB){

            try{
                Account newUser = findUser();

                if((changeIdText.getText().length() > 0) &&
                        (changePassText.getText().length() > 0)){
                   newUser.setID(Integer.parseInt(changeIdText.getText()));
                   newUser.setPassword(changePassText.getText());
                   
                for(Account oldAcc : lstAccount){ //lstAccount is the main list contain all the accounts
                    if(oldAcc.getName().equals(newUser.getName())){
                        lstAccount.remove(oldAcc); //remove old account
                        lstAccount.add(newUser);// add new account with changed password
                        saveData();// is it necessary to call saveData?
                        System.out.println("lll");//testing
                    }
                }
                }
            }catch(Exception e){
                System.out.println("error");
            }

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2860
  • View blog
  • Posts: 10,964
  • Joined: 15-July 08

Re: Object Serialization

Posted 11 July 2010 - 08:52 PM

Not to my knowledge... Serialization is like files...it saves its current state until you save again. So if you save, do some stuff, and then reload the saved object, it will be like it was when you saved it, not how it was modified during runtime.
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Object Serialization

Posted 11 July 2010 - 08:56 PM

Actually lots of code for nothing
The ArrayList contains only pointers to the objects it contains
If you modify an object contained in an ArrayList no need to actually save it and/or return it into the ArrayList (unless you want to save the whole modified ArrayList and what it contains to a file)
Was This Post Helpful? 1
  • +
  • -

#10 harmy01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 179
  • Joined: 15-June 10

Re: Object Serialization

Posted 11 July 2010 - 09:11 PM

Thanks both! i get the point. @pbl i have removed unused code :D
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2860
  • View blog
  • Posts: 10,964
  • Joined: 15-July 08

Re: Object Serialization

Posted 11 July 2010 - 09:12 PM

Glad I could help... :)
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Object Serialization

Posted 11 July 2010 - 09:15 PM

View Postharmy01, on 11 July 2010 - 10:11 PM, said:

@pbl i have removed unused code :D

useless you mean :^:

This post has been edited by pbl: 11 July 2010 - 09:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1