Shaher Jamal Eddin
Shaher Jamal Eddin

Reputation: 462

Can't read '.dat' files from folder

I am trying to read multiple '.dat' files one by one inside a specific folder. below is my code

public void viewEngingeer() throws Exception
    {
        File f = new File("Users");
        ArrayList<String> usersList = new ArrayList<String>(Arrays.asList(f.list()));

    int index=0;
    while (index < usersList.size()) { 
    User newUser=new User();
    FileInputStream fis = new FileInputStream("Users/"+usersList.get(index));
    ObjectInputStream ois =  new ObjectInputStream(fis);
    newUser = (User) ois.readObject();
    ois.close();
    System.out.println(newUser.getUsername());     
    index++;
    }

 }

but I am getting error on running


Exception in thread "main" java.io.InvalidClassException: oodj.User; local class incompatible: stream classdesc serialVersionUID = -7994693857260427394, local class serialVersionUID = 4996613179002222501

any ideas? Thank you

Upvotes: 0

Views: 502

Answers (1)

DmitryKanunnikoff
DmitryKanunnikoff

Reputation: 2266

Deserialization is impossible. Java can not restore object. You need add something like this:

 static final long serialVersionUID = 42L; 

in your class User and make User implements Serializable.

See more here: Serializabe

Upvotes: 1

Related Questions