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.
Related
I am writing a small program that inserts customer details in ArrayList and write it in file.
The problem is with ObjectOutputStream I was able to append data in file with turning FileOutputStream("",true). But when I try to read data with ObjectInputStream it only reads data that was inserted at first instance. But data is being added to file.
Here is the code -
public void insertCustomer() throws IOException
{
Customer1=new customerDetails("1", "Moeen4", "654654", "asdf", "coding", "student", "65464", "3210");
Customer3=new customerDetails("3", "Moeen5", "888888", "asdf", "coding", "student2", "65464", "321022");
Customer4=new customerDetails("4", "Moeen6", "654654", "asdf", "coding", "student", "65464", "7890");
_list=new ArrayList<customerDetails>();
_list.add(Customer1);
_list.add(Customer3);
_list.add(Customer4);
customersList cl=new customersList();
cl.WriteObjectToFile(files._customers, _list);
ArrayList<customerDetails>li=new ArrayList<customerDetails>();
li= (ArrayList) cl.ReadObjectFromFile(files._customers);
for(int i=0;i<li.size();i++)
{ System.out.println(li.size());
System.out.println(li.get(i).Id);
System.out.println(li.get(i).name);
System.out.println(li.get(i).annual_Salary);
System.out.println(li.get(i).Company);
System.out.println(li.get(i).dateOfBirth);
System.out.println(li.get(i).phone_Number);
}
}
public void WriteObjectToFile(String filepath,Object serObj) {
try {
FileOutputStream fileOut = new FileOutputStream(filepath,true);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(serObj);
objectOut.close();
System.out.println("The Object was succesfully written to a file");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Object ReadObjectFromFile(String filepath) {
try {
FileInputStream fileIn = new FileInputStream(filepath);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
Object obj = objectIn.readObject();
objectIn.close();
System.out.println("The Object has been read from the file");
return obj;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
The real problem here is this:
FileOutputStream fileOut = new FileOutputStream(filepath, true);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(serObj);
You cannot append to an existing serialization like this. If you do get an exception when attempting to read any objects appended to a pre-existing (non-empty) file.
There is a trick / hack that allows you to append objects though; see Appending to an ObjectOutputStream. (The trick involves suppressing the writing of the object stream header. It is most easily done by overriding the method that does this.)
The other approach is to keep the ObjectOutputStream open between writeObject calls. However there are use-cases where that won't be possible.
Note that there is a semantic difference between these two approaches. The best way to explain it is that the first one behaves as if you called reset() each time you write an object; see the javadoc.
Another thing to note about your example is that your reader code only reads one object. If you want to read multiple objects, you need to call readObject in a loop. And that will only work if you have used the trick / hack above to avoid writing a spurious header.
As suggested the code is only reading the first object and you would need to use a loop to read all the objects from multiple writes.
However -
If you change the above code i.e. ReadObjectFromFile to use a loop this will lead to an StreamCorruptedException: invalid type code: AC. The ObjectOutputStream constructor writes a serialization stream header to the OutputStream i.e. the file, when it is closed and reopend using new ObjectOutputStream and new FileOutputStream(filepath, true) a new header will be written at the append point so you will get an exception as the header is only expected once at the beginning of the file
This will need to be handled e.g.
Use the same ObjectOutputStream for the duration
Override java.io.ObjectOutputStream.writeStreamHeader() to account for append to a file
Change the approach and use List<List<Object>> which you could read, add, write to as a whole.
Loop example would throw exception unless ObjectOutputStream approach is changed
public Object ReadObjectFromFile(String filepath) {
List<List<Object>> objects = new ArrayList<>();
FileInputStream fileIn = new FileInputStream(filepath);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
try {
while (true) {
List<Object> obj = (List<Object>) objectIn.readObject();
// This will throw StreamCorruptedException: invalid type code: AC
objects.add(obj);
System.out.println("The Object has been read from the file");
}
} catch (EOFException ex) {
// ENDS WHEN ALL READ
} finally {
fileIn.close();
objectIn.close();
}
return objects;
}
Sudo code List<List<Object>> approach -
public void readAndWrite() {
List<Object> customer = List.of(new CustomerDetails(...),
new CustomerDetails(...),
new CustomerDetails(...));
List<List<Object>> objects = readFromFile("existing-customer-file.txt");
objects.addAll(customer);
writeObjectToFile(objects);
}
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;
}
I have a game that regularly saves the game current status to a file (by writing a serialized object to a file).The user can resume the game later on and it will read that object and build the game from there.
This seems to be working fine, but sometimes I get this exception
java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:98)
at java.io.ObjectInputStream.nextTC(ObjectInputStream.java:506)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:778)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960)
at com.myGame.GameState.loadFromFile
at com.myGame.GameState.loadGame
And here is the loadFromFile code
try {
FileInputStream fis = mActivity.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
stateDetails = (StateDetails)is.readObject();//exception is generated here
is.close();
fis.close();
} catch (Exception e) {
MyApplication.SWERR(e);
}
I don't understand why would this happen. Its one time object saving and reading. No loops involved to reach end of file. Why would it be generated?
Any help is appreciated ........thanks
Edit: this is how I do the saving
FileOutputStream fos = mActivity.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(stateDetails);
os.flush();
fos.flush();
os.close();
fos.close();
This seems to be working fine, but sometimes I get this exception
Try to use this approach:
try {
FileInputStream fis = mActivity.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
while(true) {
try {
stateDetails = (StateDetails) is.readObject();
}
catch(EOFException ex) {
is.close();
break;
}
}
}
catch (Exception e) {
MyApplication.SWERR(e);
}
I had similar problem and i solved it by code above. But always you need to make sure that:
don't call readObject() twice
close always ObjectOutputStream immediately after writing is done
if you are writing more objects at first you need to write their
count via writeInt(<value>) and then before reading call getInt() and read objects
in loop
EOFException means you are trying to read past the end of the file. Plausible causes could be:
Problem while writing the file, or possible corruption of the file itself.
It can be zero length file that shouldn't be zero length.
You might be reading a file, other than what was wrote.
My suggestion would be, put a check for is.available() and try reading the object.
I have an assignment where we are required to create a User class with a toString() method. The toString method should return a string with all the information about the user. We are required to create an array of 50 User objects and print the information about each user on a separate line. The problem is, I want to keep everything in a neat table with all Strings of the same length. So for instance:
User Name Password Full Name Email
___________________________________________________________________________________________
shortUser 12345 John Smith jSmith#shortnames.com
thisUserIsLonger 1234567890 Smitty Werbenjagermanjensen Smitty#thisOneIsLong.com
I would like to keep everything aligned as it is in the above table. This would be easy in C++ since I could just use setw() to dynamically pad spaces between according to the size of the field. Is something like this possible in Java?
I know this may not be the exact thing you are looking for, but if you were comfortable with printf and its siblings, take a look at the String.format()
There are many was to store the data and retrieve as you prefered
eg : store in DBMS or serialize the data
As there is not much data I would stick to serialization.
First you need to create person been class.
class Person{
String name;
String pwd;
String email;
String fullName;
// make the getters and setters
}
Set Person data using setters (you may use a loop)
Add the object person to a ArrayList
arrayListObj.add(person1);
serialize the ArrayList
try {
System.out.println("serializing list");
FileOutputStream fout = new FileOutputStream("list.dat");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(arrayListObj);
oos.close();
}
catch (Exception e) {
e.printStackTrace();
}
Deserialize as follows
try {
FileInputStream fin = new FileInputStream("list.dat");
ObjectInputStream ois = new ObjectInputStream(fin);
List<String> list = (ArrayList) ois.readObject();
for (String s : list){
System.out.println(s);
}
ois.close();
}
catch (Exception e) {
e.printStackTrace();
}
After deserializing you may have to use iterator to invoke the list
These are the basic steps that you could follow to achieve your target!
After words it is just about the print the result.
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.