How to retrieve many objects from .dat File using Java? -
I have an administrator account that should be enabled in the multiple users by adding to the .dat file . Then I want to retrieve all the objects from the .dat file for further programming.
Implements the public category user serializable {// meet and set methods}
this is hwo I am writing every object in the .dat file
Public Zero addNewUser () throws exceptions {user newUser = new user (); NewUser.name = "test"; NewUser.position = "administrator"; FileOutputStream Outstream = New FileOutputStream ("Users.dat", true); ObjectOptputWatchStream objectOptputableFile = new objectOutputstream (outstream); Write the object to the file. ObjectOutputFile.writeObject (NEWUSER); // close the file objectOutputFile.close (); }
How to recover all objects from the .at file in ArrayList ??
public class displayUsers {** // what to do ?? You can write list items and read it as a list. But since you are writing user objects individually, you can do something like this - ObjectInputStream OIS = New Object Instestream (new file inputstream ("user dot"); Object object = nose; While ((object = ois.readObject ())! =) {If (object installation user) {user user = (user) object; List.add (user); }}
Of course, you will need to take care of exceptions (like EOFException).
Comments
Post a Comment