I have an ArrayList of Employee objects where Employee class implements Serializable. I am using this code to write lists to a file:
ArrayList<Employee> empList = new ArrayList<>();
FileOutputStream fos = new FileOutputStream("EmpObject.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// write object to file
empList .add(emp1);
empList .add(emp2);
oos.writeObject(empList);
empList .add(emp3);
oos.writeObject(empList);
}
If I try to de-serialize it I am just getting first two objects not the 3rd one. Can anyone please try why is it?
edit1: If I add all elements at once everything is fine but not the way I did first. What is the difference?
ArrayList<Employee> empList = new ArrayList<>();
FileOutputStream fos = new FileOutputStream("EmpObject.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// write object to file
empList .add(emp1);
empList .add(emp2);
empList .add(emp3);
oos.writeObject(empList);
}
After this I have 3 elements
As GhostCat and uaraven already mentioned reset does not what you are expecting it to do and you should have a look at a tutorial on serialization and maybe consider using sth. else if this isn't fitting your use case.
Your code could look as follows if creating a new FileOutputStream:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class SerializationTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String path = "EmpObject.ser";
ArrayList<Employee> empList = new ArrayList<>();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
empList.add(emp1);
empList.add(emp2);
oos.writeObject(empList);
empList.add(emp3);
// Create a new FileOutputStream to override the files content instead of appending the new employee list
oos = new ObjectOutputStream( new FileOutputStream(path));
oos.writeObject(empList);
ObjectInputStream objectinputstream = new ObjectInputStream(new FileInputStream(path));
List<Employee> readCase = (List<Employee>) objectinputstream.readObject();
System.out.println(readCase);
}
}
What happens with your code:
you write the list into the file, with two entries
you reset the stream
you write the list again, with three entries
Thus your file contains two values, yes. Two lists, one with 2, one with 3 entries.
In other words: reset() doesn't reset what has been written to the file! You wrote one list with two entries. You are only resetting the information about stored objects, so that emp1 and emp2 get serialized completely again. Without the call to reset, the JVM would understand that it doesn't need to fully serialize emp1 and emp2 again.
Meaning: by default the JVM compresses the amount of data to transmit. It remembers which objects where already written, and instead of writing them repeatedly, it only writes something like "object X that was serialized earlier on comes again" into the stream.
So: I think you simply do not understand the point of the reset() method. Solution: read a small tutorial, like the one from tutorialspoint.
Edit given the latest comment by the OP:
What you ask for isn't possible in this way. You are writing list objects. That means that all entries of that list at that point get written to the file. The JVM remembers "that list has been written already", so it will not write it again, even when its internal state changed in the meantime.
Basically ObjectOutputStream remembers objects that are written to it. If the same object (by reference) is written again, it is not serialized, but rather a reference to previous serialized data is written to stream. reset() method cleans up internal data structures of ObjectOutputStream and allows you to write the same object again. reset() does not discard data already written to the stream.
If you try to deserialize your stream into two ArrayLists, you'll get one with two elements and one with three elements.
If you remove call to reset() method, then you will get two array lists with two elements (one actually serialized, and another as a reference to the previous serialized instance)
Related
I have an ArrayList like this one:
savedQuestions = new ArrayList<String>();
How to save it to Text file in Android local Storage When Click button ?
Even though the ArrayList class is serializable by default you will have to make the Figur class (and any classes it uses) Serializable as well. this would probably mean something like:
class Figur implements Serializable {
// To handle the version of your class
private static final int serialVersionUID = 1L;
// Your code would go here
}
And then you have to serialize with something like:
ObjectOutputStream fileOut = new ObjectOutputStream(new FileOutputStream("file"));
fileOut.writeObject(list);
fileOut.close();
And to deserialize:
ObjectInputStream fileIn = new ObjectInputStream(new FileInputStream("file"));
list = (ArrayList) fileIn.readObject();
fileIn.close();
Also note, when you write to the file, you want to append to the previous items in the file and not overwrite (errase) them.
I think either objectOutputStream() or writeObject() probably has an optional argument in it to allow it to append instead of overwrite.
Example: writeObject(list, true) instead of writeObject(list). You'll have to research this to determine the correct way to do it.
Also, if you cant get serialization to work, you can instead store the values for "circle, rect, line, color, fill" that makes up one figur object in the file as a single line with a delimiter such as 'comma'' between them. Then read a line back from the file and use those values to make a populated figur object.
Example:
Store these strings in the file:
3,6,7,red,4
6,3,4,blue,8
Then when you read the contents of the file, build your objects:
Figur figure1 = new Figur("3","6","7","red","4");
Figur figure2 = new Figur("6","3","4","blue","8");
ArrayList<Figur> figurs =new ArrayList<Figur>();
figurs.add(figure1);
figurs.add(figure2);
Its not as efficient as using serialization, but it gets the job done and the contents of the file is in a human readable form.
From the link https://community.oracle.com/thread/1193052?start=0&tstart=0
I'd like to know why my program is reading only 1. element of written objects.
I have 2 classes:
public class Sheet implements Serializable{
int something1;
String something2;
}
next one:
public class Book implements Serializable{
ArrayList<Sheet> menu = new ArrayList<Sheet>();
public void newSheet(Sheet temp)
{ menu.add(temp);}
}
Saving Book (in class main Book is static Book Libro = new Book();)
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream ("libro.obiekt"));
out.writeObject(Libro);
out.close();
and reading:
ObjectInputStream in = new ObjectInputStream(new FileInputStream("libro.obiekt"));
Libro = (Book) in.readObject();
in.close();
When I do this, while having for example: 5 objects in ArrayList inside Libro, I can read only first one, while other 4 would be NULL objects....
Any idea what am I doing wrong?
You can only read one object from file (serialized).
The reasons:
Each time you write object to file. you are overwritting it. so you
can only get the last object written.
even if you do set append to true
FileOutputStream fout = new FileOutputStream(new File(file),true);
//setting the append to true
deserializing will cause
java.io.StreamCorruptedException: invalid type code
To overcome it:
1. You could put all your objects in a list and write it as a whole (your arraylist).
serialize list of objects as a whole and deserialize it.
2. you can write each object in different file and read from it.
I have some input that I add to a serialized object.
Now when I read the serialized object, I want to check if it exists... If not loop till it has a value in it.
How do i modify the deserialization function to handle that.
There is basically a delay in populating my serializable object. So in the meantime if i were to read that object, it is going to be empty. I want to put a check to read only when it has data in it. if not it should wait till it has some data
public String _displayResults(){
String SomeData = "";
try {
FileInputStream fis = new FileInputStream("SomeDataobj");
ObjectInputStream ois = new ObjectInputStream(fis);
SomeData = (String)ois.readObject();
ois.close();
}
catch(Exception e) {
System.out.println("Exception during deserialization: ");
}
return SomeData;
}
What I tried:
added a wait condition for 2 secs for 10 times... Is there a cleaner way.
while ( ois.readObject().toString().equalsIgnoreCase("") && i <10){
Thread.sleep(2000);
i++;
}
Java provides an API called Externalizable, which allows you to customize the (de) serialization. Serialiazable is marker interface and that indicates the object can be wrote to output stream. Externalizable provides two methods readExternal() and writeExternal() where you can override the behavior.
Your question is not so clear about what you want to achieve, so I am not sure if the above information is helpful for you
This question already has an answer here:
StreamCorruptedException: invalid type code: AC
(1 answer)
Closed 5 years ago.
I was trying out a program that was given in the exercise at the end of the chapter 'Serialization'.
The program requires me to declare a class Person which encapsulates only two data members of type Name and Address , which are also classes.
Then I have to take a series of names and addresses from the keyboard , create objects and write them to the file.
However , if the FILE ALREADY EXISTS then the objects must be APPENDED to the existing file.
My program runs perfectly for the first time but for the second time , when I try to read back the appended records , I get an Exception
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at Trial.main(Trial.java:66)
I did my bit of research on this and found that the StreamHeader can be written ONLY ONCE and appending corrupts it.
What is the way around it ???
The Object Writing Code is:
try(ObjectOutputStream stream = new ObjectOutputStream(new BufferedOutputStream(Files.newOutputStream(filePath,WRITE,CREATE,APPEND)))) {
for(int i = 0;i<name.length;i++) {
Person aPerson = new Person(name[i],address[i]);
System.out.println(aPerson);
stream.writeObject(aPerson);
stream.reset();
aPerson = null;
}
System.out.println("Writing Complete");
Yes, I've had this problem myself before... it is not possible, unfortunately.
What you could do is to place your objects into a List and persist the full list at a time. As the list is an object it can persisted just as easily. I know this is terrible as this require the entire contents to be read into memory, but it is the only way as far as I know.
The other option (which I recommend) is that you use something like JSon to commit your data. GSon works quite well for this purpose. You can then simply marshall and unmarshall your objects which can be committed to a text file. It's very easy to do as a single line of code is required to go either way (object to JSon-string and vice versa).
This works. So debug your program and see why it doesn't. Probably don't call reset()
public class ClassTest {
public static void main(String a[]) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:/temp/foo.txt"));
oos.writeObject(new Test("foo", "bar"));
oos.writeObject(new Test("baz", "brr"));
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c:/temp/foo.txt"));
System.out.println(ois.readObject());
System.out.println(ois.readObject());
ois.close();
}
}
class Test implements Serializable {
private String fld1;
private String fld2;
public Test(String v1, String v2) {
this.fld1 = v1;
this.fld2 = v2;
}
#Override
public String toString() {
return "Test [fld1=" + fld1 + ", fld2=" + fld2 + "]";
}
}
friends,
i am facing a problem i have phoneContacts list with name and phone numbers in it.
i want to copy it in two different static lists so that i can use it to other activities. i am using following code but it displays me last list references in both while retrieving data
any one guide me how can i take separate copies of these two objects ?
MyContacts.attackContacts = new ArrayList(phoneContacts);
Collections.copy(MyContacts.attackContacts,phoneContacts);
MyContacts.attackContacts.get(0).setType("attack");
MyContacts.medicalContacts = new ArrayList(phoneContacts);
Collections.copy(MyContacts.medicalContacts,phoneContacts);
MyContacts.medicalContacts.get(0).setType("medical");
System.out.println("attack" + MyContacts.attackContacts.get(0).getType() + " medical " + MyContacts.medicalContacts.get(0).getType());
// result "attack medical" "medical medical"
// it should show independent list results like "attack attack" "medical medical"
any help would be appreciated.
In this case you would need to make a deep copy of the list, i.e., a copy that doesn't copy references, but actually copies the object the references are pointing at.
Collections.copy "copies all of the elements from one list into another". As usual with Java however, the elements of a list are not objects but references.
You could solve this by implementing Cloneable (and using .clone()) or by creating a custom "copy constructor" which takes a to-be-copied object as argument, and creates a new object based on the data of that argument. No matter which option you choose, you'll have to iterate over the list and perform the copy on each object.
Here's an example that uses the copy-constructor approach:
MyContacts.medicalContacts = new ArrayList();
for (Contact c: MyContacts.attackContacts)
medicalContacts.add(new Contact(c)); // add a copy of c.
Related question:
What is the difference between a deep copy and a shallow copy?
Coping the list, creates new list object that still refers to the same element objects.
To make a deep copy, your element object must be clonable, have copy constructor, or some other way to duplicate it and you have to do the copy in a loop, one by one.
for (Elem x: list1) {
list2.add(copyOf(x))
}
Pass the object whcih you wants to copy and get the object which you wants ,
private Object copyObject(Object objSource) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(objSource);
oos.flush();
oos.close();
bos.close();
byte[] byteData = bos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(byteData);
try {
objDest = new ObjectInputStream(bais).readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return objDest;
}
Cast you objDest to Desiered object