Saving an ArrayList of various objects in an efficient way - java

I would like to know how to save an ArrayList of abstract Objects to a file.
So far I only save primitive types or ArrayLists of primitive types by converting them to a comma separated String and storing this with a buffered reader.
But now I have got an ArrayList of Game Elements, which have really different properties and Constructors, so my normal approach won't work. There has to be something nicer than storing each to a file or each type of Object to a file or add plenty of seperator levels.
How do I do this in a nice way?

Have a look at Serialization, there are plenty of tutorials out there so I am not going to post any code:
http://www.tutorialspoint.com/java/java_serialization.htm

You can not instantiate Abstract Objects so you will need a child class which extends it. Also Abstract class should implement Serialize. Then using ObjectOutputStream you can directly write ArrayList using writeObject() method.
Below is the sample application
public abstract class Parent implements Serializable {
public abstract String getValue(); //Just to show value persist
}
public class Child extends Parent {
String value = null;
Child(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
// No throws clause here
public static void main(String[] args) throws FileNotFoundException,
IOException, ClassNotFoundException {
//create Arraylist
ArrayList<Parent> parents = new ArrayList<Parent>();
parents.add(new Child("test"));
//store
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
new FileOutputStream("test.txt"));
objectOutputStream.writeObject(parents);
objectOutputStream.close();
//Read back
ObjectInputStream objectInputStream = new ObjectInputStream(
new FileInputStream("test.txt"));
ArrayList<Parent> readObjects = (ArrayList<Parent>)objectInputStream.readObject();
System.out.println(readObjects.get(0).getValue());
}

The answer could be two.
Depending on what is the usage of the file later.
ANS 1: It you want the object values to be saved temporarily in the file and reload it again from the file, then serialization is the best options.
ANS 2: If the file is output of the program and then you try the below
option#1: Start each line in the file with the unique object name
OBJECT1, blue, pink, yellow....
OBJECT2, rose, dairy, sunflower, cauliflower..
option#2 instead of the flat file(txt) you can use an apache poi framework to write
the object in more organised way.

Related

Managing Files in java Read,Save,Write

I would like to write a general method readfile() to a class to use it on the subclasses to read all lines.
I have trouble with how to call methods from one class to another.
First, is it better to make void readfile() ?
or return a File?
Secondly, which is the way to reused from other classes?
Example of my code:
public class Reader{
Scanner myReader = new Scanner(myObj);
public void readFile(){
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
}
}
public class ReadContentOfFile extends Reader{
public List<String> parseFile{
List<String> name = new ArrayList<>();
//how to get lines? as I have them, from
another method?
//for example If I want to get the
words,separated, by comma
return name;
}
}
public void Main(){
}
For example for each line of a file, how I get each element.
My problem is about how to get the data for another method, for another class.
First, is it better to make void readfile() ? or return a File?
Depends on what you want to do with the output, since you are printing the contents to console then you don't have to return the file and void is OK.
But if you wanted to use this file after you call the readFile method then you must return it or set it to class member.
Secondly, which is the way to reused from other classes?
make your method static, so you can access it without creating an object since it's just a utility and object is not important here.
public static void readFile(){
//..
}
then do
Reader.readFile()
Please, do not use Scanner to read a file, there are simpler and better options.
First, a File itself it's nothing but a reference and it won't contain any content
related to the file on system. So, you can consider to return a File object only if you may need some information from the file-system about privileges, existence, perform deleting actions or retrieving path information.
Usually when i write methods to read() or save() a file i make them void.
About how do you read data and access it, in java there are a lot of possibilities.
I'll show you one pretty straightforward:
List<String> lines = Files.readAllLines(Paths.get("filename.txt"), StandardCharsets.UTF_8);
And that's it but there are more options.

Store text file in non-String array

I am trying to store lines from a text file into an array that isn't a String array. The return type is supposed to be a GO-array ( have created another class named "GO" with constructors and get-/set-methods which I'll use in other parts of the code.)
int rad = 0;
GO [] list = new CD [rad];
public GO[] readFile(String filename) throws FileNotFoundException {
Scanner read = new Scanner (new File(filename));
while (read.hasNextLine()==true){
rad++;
read.nextLine();
} read.close();
read = new Scanner(new File(filename));
for (int i=0;i<list.length;i++){
//here the lines of the file should be stored in the "GO"array
// but the declaration cannot convert string to GO[]
list[i]=read.nextLine()
class GO<T>{
private T ref;
public GO(T ref) {
this.ref = ref;
}
}
you should change your GO class to use generics and you can add value to Go Array with this approach.
list[i] = new GO(read.nextLine());
by changing the class to generics . you can create an instance of Go class by passing any object to the constructor.
You'll need to parse the resulting String yourself and build a GO object out of it, which can be a separate method to keep things clean. Get the values of your object's variables and use your setters appropriately.
If your program is what's writing the file's contents, you'd be better off having GO implement Serializeable and saving/loading the state of the object itself instead of making text. If not, the above is your only option, really.

serialization/deserialization mechanism

Say, I have a class X which has a field value, that is,
class X implements Serializable {
private int value;
// ...
}
Further it has getters and setters not displayed here. This class is serialized.
At the deserialzation, end same class has value field and access specifier is public. Further, this class does not have getters and setters. So, my questions are:
Does deserialization fail in case the access specifier of the field changes OR some or all of the methods go missing in the class at the deserialization end?
What is the mechanism by which fields are assigned their values during deserialization?
Some good links The Java serialization algorithm revealed
1) does deserialization fail in case the access specifier of the field
changes OR some or all of the methods go missing in the class at the
deserialization end ?
Serialization happens using Using Reflection
Java Detects the changes to a class using the
private static final long serialVersionUID
The default involves a hashcode. Serialization creates a single hashcode, of type long, from the following information:
The class name and modifiers
The names of any interfaces the class implements
Descriptions of all methods and constructors except private methods and constructors
Descriptions of all fields except private, static, and private transient
The default behavior for the serialization mechanism is a classic "better safe than sorry" strategy. The serialization mechanism uses the suid, which defaults to an extremely sensitive index, to tell when a class has changed. If so, the serialization mechanism refuses to create instances of the new class using data that was serialized with the old classes.
2) what is the mechanism by which fields are assigned their values
during deserialization ?
The real details can be read in the Java Object Serialization Specification.
To answer your questions:
Serialization has a basic sanity check to see if the serialization ends use the same version of a class: the serialVersionUID member must be equal. Read the section Stream Unique Identifiers to know more about it. Basically, it's a static value which you can either manage yourself by declaring it on your class, or let the compiler generate one for you. If the compiler generates it, ANY change to a class will result in a change of serialVersionUID and hence will make the deserialization fail if the ends do not have exactly the same classes. If you want to avoid this, declare the variable yourself and update it manually when a change to the class' member variables does make classes incompatible.
The Java Virtual Machine does a lot of the magic here, it can access all internal state directly without the need for getters (fields marked transient or static aren't serialized though). Also, while the Serializable interface doesn't specify any methods to implement, there are a number of 'magic methods' which you can declare to influence the serialization process. Read section "The writeObject Method" and onwards to know more. Be aware though that you should use these sparingly as they might confuse any maintenance developers!
You don't need to have getters/setter to serialize/deserialize using java serialization, for example, check this code:
public class Main {
public static class Q implements Serializable {
private int x;
public Q() {
x = 10;
}
public void printValue() {
System.out.println(x);
}
}
public static void main(String[] args) throws Exception {
Q q = new Q();
FileOutputStream fos = new FileOutputStream("c:\\temp.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(q);
fos.close();
FileInputStream fis = new FileInputStream("c:\\temp.out");
ObjectInputStream oin = new ObjectInputStream(fis);
Q q2 = (Q)oin.readObject();
fis.close();
q2.printValue();
}
}
I don't really know how you get this results, but what you tell is not the default behaviour of serialisation. So, I guess you are using it wrong. Here is some sample code:
public class X implements Serializable
{
private int value;
public int getValue() { return value; }
}
Here the serialisation/deserialisation process:
X x = new X();
x.setValue(4);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputSteam(buffer);
oos.writeObject(x);
oos.flush();
oos.close();
ByteArrayInputStream in = new ByteArrayInputStream(buffer.toByteArray());
ObjectInputStream ois = new ObjectInputStream(in);
Object obj = ois.readObject();
if (obj instanceof X)
{
X readObject = (X) obj;
System.out.println(readObject.getValue());
}
You probably used Java Reflection to get your results. Make sure you use getDeclaredFields(); and getDeclaredMethods(); instead of the variants without Declared in the method name.
Does deserialization fail in case the access specifier of the field changes
No.
OR some or all of the methods go missing in the class at the deserialization end?
Yes, unless the receiving class has a serialVersionUID member whose value equals the value encoded in the stream.
what is the mechanism by which fields are assigned their values during deserialization?
Too broad, but:
Reflection, and
name matching (rather than matching by position in the class and stream).

create a common converter for objects from different packages

I have 5 webservices, A, B, C, D, and E. Each has autogenerated objects of the exact same structure, but with different names and in different packages.
com.ws.a.carA contains parameters and com.ws.a.wheelA
com.ws.b.carB contains parameters and com.ws.b.wheelB
com.ws.c.carC contains parameters and com.ws.c.wheelC
com.ws.d.carD contains parameters and com.ws.d.wheelD
com.ws.e.carE contains parameters and com.ws.e.wheelE
I want to create one function that can convert each of these objects (and the inner wheel) to a object named
com.model.car,
but I dont wan't many functions like :
com.model.car convert(com.ws.a.objA obj)
com.model.car convert(com.ws.b.objB obj)
...
The problem is, I can't give all the objects a common interface to implement, because I don't want to manually change the autogenerated classes (they are recreated frequently).
I need a way, probably with generics, to create a common function
com.model.car convert(T obj)
that will work for all the car types but I'm not sure how to implement it.
You can use reflection for this. The easiest and cleanest way would probably be to use Apache Common BeanUtils, either PropertyUtils#copyProperties or BeanUtils#copyProperties.
PropertyUtils#copyProperties copies the values from one object to another, where the field names are the same. So with copyProperties(dest, orig), it calls dest.setFoo(orig.getFoo()) for all fields which exist in both objects.
BeanUtils#copyProperties does the same, but you can register converters so that the values get converted from String to Int, if necessary. There are a number of standard converters, but you can register your own, in your case com.ws.a.wheelA to com.model.wheel, or whatever.
You can also check out Dozer
I think you should consider using reflection.
Using commons beanutils library you may do this utility class:
public class BeanUtilCopy {
private static BeanUtilsBean beanUtilsBean;
private static ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
static {
convertUtilsBean.register(new Converter() { //2
public <T> T convert(Class<T> type, Object value) {
T dest = null;
try {
dest = type.newInstance();
BeanUtils.copyProperties(dest, value);
} catch (Exception e) {
e.printStackTrace();
}
return dest;
}
}, Wheel.class);
beanUtilsBean = new BeanUtilsBean(convertUtilsBean);
}
public static void copyBean(Object dest, Object orig) throws Exception {
beanUtilsBean.copyProperties(dest, orig); //1
}
When (1) beanUtilsBean use the converter (2) to pass the Wheel**X** values to the Wheel in destination bean.
Use sample:
CarB carB = new CarB();
carB.setName("car B name");
carB.setWeight(115);
WheelB wheelB = new WheelB();
wheelB.setName("wheel B name");
wheelB.setType(05);
carB.setWheel(wheelB);
Car car1 = new Car();
BeanUtilCopy.copyBean(car1, carB);
System.out.println(car1.getName());
System.out.println(car1.getWeight());
System.out.println(car1.getWheel().getName());
System.out.println(car1.getWheel().getType());
The output:
car B name
115
wheel B name
5

Java serialization - incompatible serialVersionUID

I understand the theory behind incompatible serialVersionUIDs (i.e. you can discriminate different compilation versions of the same class) but I am seeing an issue that I don't understand and doesn't fall into the obvious error causes (different compiled version of the same class).
I am testing a serialization/deserialization process. All code is running on one machine, in the same VM, and both serialization and deserialization methods are using the same version of the compiled class. Serialization works fine. The class being serialized is quite complex, contains a number of other classes (java types and UDTs), and contains reference cycles. I haven't declared my own UID in any class.
This is the code:
public class Test {
public static void main(String[] args) throws Exception {
ContextNode context = WorkflowBuilder.getSimpleSequentialContextNode();
String contextString = BinarySerialization.serializeToString(context);
ContextNode contextD = BinarySerialization.deserializeFromString(ContextNode.class, contextString);
}
}
public class BinarySerialization {
public static synchronized String serializeToString(Object obj) throws Exception {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(byteStream);
oos.writeObject(obj);
oos.close();
return byteStream.toString();
}
public static synchronized <T> T deserializeFromString(Class<T> type, String byteString) throws Exception {
T object = null;
ByteArrayInputStream byteStream = new ByteArrayInputStream(byteString.getBytes());
ObjectInputStream in = new ObjectInputStream(byteStream);
object = (T)in.readObject();
in.close();
return object;
}
}
I am getting an InvalidClassException (local class incompatible: stream classdesc serialVersionUID = -7189235121689378989, local class serialVersionUID = -7189235121689362093) when deserializing.
What is the underlying issue? And how should I fix it?
Thanks
Edit
I should state the purpose of this. The serialized data will both need to be stored in a sqlite database and sent across the wire to other clients. If String is the wrong format for passing around the serialized data, what should I be using instead that will let me store and pass the data about? Thanks again.
First rule: never use String or char[] or Reader or Writer when handling binary data.
You're handling binary data and try to put it into a String. Don't do that, that's an inherently broken operation.
Next: the return value of byteStream.toString() doesn't in any way represent the actual content of the ByteArrayOutputStream. You'll want to use .getBytes() and pass the byte[] around (remember: treat binary data as binary data and not as a String).

Categories