I have two POJO with same setter and getter now i want to copy all the bean value to another bean. What can be the best way to copy all the information of one POJO to another.?
U can use Apache BeanUtils ...
That's the perfect situation to use a Java Bean mapper like orika or Dozer. They can automate this task pretty well and you can avoid a lot of code.
You can basically copy all properties of random classes to other classes if they have the correct getters and setters with just a single line.
Implement deep copy here. It will be best as all sub POJO's inside POJO will also be copied and there will be no loss.
public CloneExample deepCopy() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return (CloneExample) ois.readObject();
} catch (IOException e) {
return null;
} catch (ClassNotFoundException e) {
return null;
}
Related
I might be trying to do this the hard way so let me know if there is a better solution.
I am making a simple text game in Java which you select your actions by a GUI. I have a couple of classes I am trying to serialize one being the player and another being an NPC. Is there an easy way to serialize more then one object (player and NPC) into the same file? I can serialize one object and load it back into the game.
Am I going about this the wrong way? Is there a simpler way of trying to save the game state?
If I have a class that creates multiple objects and I serialize that class, will the objects it created be serialized as well?
Thanks
An alternate approach to writing objects sequentially is to store them in a collection (e.g. a HashMap), since collections can be serialized. This may make it a little easier to manage upon retrieval, especially if you have many objects to serialize/deserialize. The following code demonstrates this:
String first = "first";
String second = "second";
HashMap<String, Object> saved = new HashMap<String, Object>();
saved.put("A", first);
saved.put("B", second);
try {
FileOutputStream fos = new FileOutputStream("test.obj");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(saved);
oos.flush();
oos.close();
fos.close();
FileInputStream fis = new FileInputStream("test.obj");
ObjectInputStream ois = new ObjectInputStream(fis);
HashMap<String,Object> retreived = (HashMap<String,Object>)ois.readObject();
fis.close();
System.out.println(retreived.get("A"));
System.out.println(retreived.get("B"));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Running this should result in:
first
second
Just call writeObject() as many times with as many different objects as you need, and conversely call readObject() ditto.
Hard to believe you haven't already tried it.
org.apache.cxf.jaxrs.ext.multipart.Attachment attachments=null;
List<Attachment> clone = new ArrayList<Attachment>(attachments.size());
for(Object item: attachments)
clone.add((Attachment)item.clone());//The method clone() from the type Object is not visible
I want to clone the List<Attachment> clone object but it saying The method clone() from the type Object is not visible
Do not use Java standard clone method from Object. At least not before you read what Josh Bloch says about cloning in his book Effective Java.
You need to do a deep copy in this case i.e create an empty ArrayList of Attachment objects (which you did) and copy the values of all the fields of every Attachment object.
A deep clone sample
public static Object deepClone(Object model) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(model);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
}
catch (IOException e) { .. }
catch (ClassNotFoundException e) { .. }
finally { /*close stream*/ }
}
clone.add((Attachment)item.clone());//The method clone() from the type Object is not visible
The actual problem with this code is an operator precedence issue. Assuming the actual object is cloneable, and ignoring for the moment the issue of whether you should be cloning it at all, it should be written:
clone.add((Attachment)((Attachment)item).clone());
Replace the last line as:
clone.add(((Attachment)item).clone());
Make sure the Attachment implements the interface Cloneable
Hi I have an issue when trying to append new objects to the existing file..
Once the android app has been lunched again I want to get the existing file and add a new objects then read the objects from the existing file ... Actually, when I'm trying to read the object, the code will read only the first objects .. You can find below the code .. Could you please help ? Thanks
using the following method to write an objects :
public void saveObject(Person p, File f){
try
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f, true));
oos.writeObject(p);
oos.reset();
oos.flush();
oos.close();
}
catch(Exception ex)
{
Log.v("Serialization Save Error : ",ex.getMessage());
ex.printStackTrace();
}
}
Using the following method to read an objects :
public Object loadSerializedObject(File f)
{
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
try{
Object loadedObj = null;
while ((loadedObj = ois.readObject()) != null) {
Log.w(this.getClass().getName(), "ReadingObjects") ;
}
return objects;
}finally{
ois.close();
}
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
Unfortunately you can't create a new ObjectOutputStream every time you want to append to the stream and then read everything back with a single stream. The constructor adds headers to the underlying stream before you start writing objects. You are probably seeing the java.io.StreamCorruptedException: invalid type code: AC exception, that's because the first header is 0xAC.
I don't know how many objects you are dealing with, but one option might be to read all your objects and then rewriting them all using a single ObjectOutputStream. That can get pricy if there are lots of objects. Alternatively, you might want to consider managing the serialization yourself manually through Externalizable. It can get painful though.
I wrote this function which will attempt to store the map but its not working I think? I am using netbeans and each time i go to the directory of project in the java src i can't find the created file or anywhere else in the project. The map is surely valid because output comes out perfect when am not dealing with storage. btw I do implement seriliazable :)
Note: the map is of type TreeMap
public boolean storeMap(TreeMap<DateTime, Integer> map){
try{
f_out = new FileOutputStream("mapObject.data");
obj_out = new ObjectOutputStream (f_out);
obj_out.writeObject(map);
return true;
}catch(IOException ioe){
System.err.print(ioe);
return false;
}
}
is there a reason why the output file is not generated?
Thanks
I suggest to use absolute path, that is something like
f_out = new FileOutputStream("/home/username/mapObject.data");
or on windows
f_out = new FileOutputStream("c:\\work\\mapObject.data");
If there was no exception thrown (System.err.print(ioe); this line did not print anything) then the file was created somewhere.
SERIALIZE A HASHMAP:
This code is working fine , I have implemented and used in my app. Plz make ur functions accordingly for saving map and retrieving map.
Imp thing is, you need to make confirm that the objects you are putting as value in map must be serializable , means they should implement serailizbele interface. ex.
Map<.String,String> hashmap=new HashMap<.String,String>().. here in this line ...map and string both are implictly serializable , so we dont need to implement serializble for these explicitly but if you put your own object that must be serializable.
public static void main(String arr[])
{
Map<String,String> hashmap=new HashMap<String,String>();
hashmap.put("key1","value1");
hashmap.put("key2","value2");
hashmap.put("key3","value3");
hashmap.put("key4","value4");
FileOutputStream fos;
try {
fos = new FileOutputStream("c://list.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(hashmap);
oos.close();
FileInputStream fis = new FileInputStream("c://list.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Map<String,String> anotherList = (Map<String,String>) ois.readObject();
ois.close();
System.out.println(anotherList);
} catch (FileNotFoundException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
} catch (ClassNotFoundException e) {e.printStackTrace();
}
}
Try to call fulsh() method for outputStreams.
obj_out.flush();
f_out.flush();
and close them in finally statment.
I have created a method in my java assignment to write into a file from a LinkedList (I used serialization) , then I have created another method to read the file into the inkedList. The following is my method's body:
try {
FileInputStream fin = new FileInputStream("c:\\Info.ser");
ObjectInputStream ois = new ObjectInputStream(fin);
Employee e = (Employee) ois.readObject();
linkP.add(e);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but it doesnt work right. I think this part:
Employee e = (Employee) ois.readObject();
linkP.add(e);
reads only the first object of the file into the linkedlist and ignores other objects. I surrounded it for loop and while loop several times but it causes EOFException. How can I change my method to read all of the file's objects into the LinkedList?
If you used LinkedList for serialization you should expect a LinkedList to deserialize:
linkP= (LinkedList) ois.readObject();
instead of
Employee e = (Employee) ois.readObject();
linkP.add(e);
The easiest way is to include the size of the list as the first thing written to the file. When you read the file, the first thing you retrieve is the size. Then you can read the expected number of objects.
Are you sure that the serialized file contains all of the elements? It looks to me like you might only be serializing one.
Note: Please also add the code where you create the info.ser file, since you may have corrupted the ObjectOutputStream by closing/reopening it for each object.
But to answer your question, the proper way of doing it (without catching exceptions) would be:
#Test
public void testSerializingListByEntries() throws Exception {
List<Serializable> list = new ArrayList<Serializable>();
list.add(new Date());
list.add(new Date());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeInt(list.size()); // Magic
for(Serializable o : list) {
oos.writeObject(o);
}
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
int count = ois.readInt();
List<Object> newList = new ArrayList<Object>();
for(int i = 0; i < count;i++) {
newList.add(ois.readObject());
}
ois.close();
assertEquals(list,newList);
}
Yes, you need to close the streams yourself of course. Omitted for readability.
Would probably need to see how you're writing in the first place but generally:
ObjectInputStream is = null;
try
{
is = new ObjectInputStream(new FileInputStream("c:/Info.ser"));
Object object = null;
while ((object = is.readObject()) != null)
{
linkP.add(object);
}
}
catch(Exception e)
{
//Whatever you need to do
}
finally
{
//Never forget to close your streams or you'll run into memory leaks
try
{
if (is != null)
{
is.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
Also, its probably better practice for you to handle the exceptions individually but I can't really tell what the streams throw so replace the (Exception e) with everything else.
surround Employee e = (Employee) ois.readObject();
linkP.add(e);
with a for loop as you suggested and surround the .readObject call with a try/catc(EOFException)
Just catch EOFException separately inside your reading loop and process it accordingly, i.e. break out of the loop.