![]() |
| ||
| Modifying a file. I need to make a seating reservation system for my assignment and I have to manage .dat files. I have to store all Student objects in a file. When a student is registered, a new object is created and I need to add it to the archive. But as far as I know, you can not edit a serializable file in a "direct" form, so what I am trying to do is to generate an array of objects (temporary) with all the objects from the file, enter the new object registered and copy each object from the array to the file again. I have the methods to do that but I don't know where to call the methods to perform the operations. Also I don't know in which class do I have to create the ArrayList. Here is my code, I just put the classes relevant to my question: import java.io.*; import java.io.*; I translated from Spanish to English some parts of the code so you understand it with more precision. Thanks for the help in advance. |
| ||
| Re: Modifying a file. Making a class serializable in java means that you can read it from and write it to a file in binary form. So when your program exits, that is when you should be writing all of your objects to the file. When your program starts, you read them in from the file. When you read from a "serializable file", it creates your Objects for you. When you write to a "serializable file" you are essentially storing your Objects in a special form so that they can be read back in, as Objects, when you start your program the next time. I don't know a whole lot about the details of how it works (for example, I'm not sure when or how exactly the Objects are created), but that's the general idea. Hope that helps. If not, and you were already aware of that, I'll be glad to help you work out the details once you reply. |
| ||
| Re: Modifying a file. Yes I understand the mechanism about writing/reading objects from a file. But basically this is my problem: - I have a file that has objects in it. - I want to write a new object in that file when I run the program, but if I do that "directly" using the writeObject, then the other objects will be lost (overwriting file). - So what I am doing is copying every object from the file to an array of objects, so there will no lost objects after writing the new one. Then I copy each object from the array to the file. I have the methods to do that (as you can see in my Estudiante class) but I don't know how to call the methods in Main class to perform this actions. Estudiante.generateBackup(); >> this line is working OK (line 23) Estudiante.writeBackup(); >> this is causing an error when I compile (line 44): ERROR: Main.java:51: writeBackup(java.util.ArrayList<Student>) in Estudiante cannot be applied to () Estudiante.writeBackup(); Thanks for your reply. |
| ||
| Re: Modifying a file. You'll probably have to reposition your read-stream cursor after each object read, otherwise you won't retrieve the right byte value from the file to match the data type(s) for the Object you are creating. |
| ||
| Re: Modifying a file. Just a thought, instead of writing each object after serializing one by one to the file, why don't you just serialize the entire ArrayList and write it to a file. And when you need to add any data to the file, get the ArrayList back from the file, add the object to the ArrayList and serialize this ArrayList overwrite the older file with the file containing the new ArrayList |
| ||
| Re: Modifying a file. Quote:
//... necessary imports |
| ||
| Re: Modifying a file. I felt a bump was in order for this one... > Unfortunately, the implementation of ArrayList<T> looks > [something] like this-- Doesn't seem unfortunate to me; as long as the state is persisted and can be recovered from the flattened representation of the object, it doesn't matter which approach is taken to serialize the object. In this case, the implementer chose not to serialize the array which backs the ArrayListbut the individual elements for obvious reasons [hint: the size of the array is not always equal to the number of elements in the ArrayList]. The only thing you need to look out for is that the objects contained in the ArrayListcan be serialized [the concrete class implements either Serializableor Externalizable]. If my memory serves me right, you posted something along the same lines in a thread you created; maybe it's time to go back and edit it. :-) |
| ||
| Re: Modifying a file. Thanks to all who replied my question. I already solved the problem and obtained a good grade from this project. |
| All times are GMT -4. The time now is 5:55 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC