0
/*
throws me the next Exception
line 66 and 179 java.lang.newInstanceationException:Angajat at java.lang.class.newInstance(unknown source)
at mySerializer.deserializare.Object(mySerializer_final).java:66
at Main1.main(myserializer_final).java.179
caused by java.lang.NoSuchMethodException:Angajat.(init)
at java.lang.class.getConstructor0(unknown source)
*/
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
class Angajat {
public String nume;
public int salariu;
private int varsta;
public Angajat(String nume, int salariu, int varsta)
{
this.nume=nume;
this.salariu=salariu;
this.varsta=varsta;
}
public String toString(){
return this.nume+ " (" + this.salariu + ")";
}
}
class MySerializer{
public void serializareObiect(BufferedWriter buffer_out, Object object){
try{
Class c=object.getClass();
buffer_out.write(c.getName()+"\n");
// preluarea si adaugarea intr-o listFields a field-urilor dintr-o anumita clasa
ArrayList<Field> listFields = new ArrayList<Field>();
Field[] field = c.getDeclaredFields(); //campuri declarate
for (Field f1 : field)
{
f1.setAccessible(true);
listFields.add(f1);
}
Field[] inherited_field = c.getFields(); //campuri publice/mostenite
for (Field f2 : inherited_field)
{
f2.setAccessible(true);
listFields.add(f2);
}
//scrierea in fisier a campurilor unui obiect
for (int i = 0; i < listFields.size(); i++) {
Field writeField = listFields.get(i);
if (writeField.get(object) != null)
buffer_out.write(writeField.get(object) + "\n");
else if (writeField.get(object) == null)
buffer_out.write("null \n");
}
buffer_out.flush();
}
catch(Exception e){
System.out.println("error");
}
}
public Object deserializareObject(BufferedReader buffer_in) throws Exception{
String name_of_class = buffer_in.readLine();
Class c = Class.forName(name_of_class);
Object serialize_object = c.newInstance();
// preluarea si adaugarea intr-o listFields a field-urilor dintr-o anumita clasa
List<Field> listFields = new ArrayList<Field>();
Field[] field = c.getDeclaredFields(); //campuri declarate
for (Field f1 : field)
{
f1.setAccessible(true);
listFields.add(f1);
}
Field[] inherited_field = c.getFields(); //campuri mostenite/publice
for (Field f2 : inherited_field)
{
f2.setAccessible(true);
listFields.add(f2);
}
for (int i = 0; i < listFields.size(); i++)
{
String class_name = listFields.get(i).getType().getName().toString();
String read_field = buffer_in.readLine();
Field f;
class_name = conversieTipuri(class_name);
if (read_field.equals("null") == false) {
if (class_name.equals("java.lang.Integer")){
f=listFields.get(i); //obtinem valoarea campului
f.set(serialize_object, Integer.parseInt(read_field)); //o setam la acea valoare!!(pentru a reface sirul de biti)
}
else if (class_name.equals("java.lang.Long")){
f=listFields.get(i);
f.set(serialize_object, Long.parseLong(read_field));
}
else if (class_name.equals("java.lang.Short")){
f=listFields.get(i);
f.set(serialize_object, Short.parseShort(read_field));
}
else if (class_name.equals("java.lang.Character")){
f=listFields.get(i);
f.set(serialize_object, read_field.charAt(0));
}
else if (class_name.equals("java.lang.Float")){
f=listFields.get(i);
f.set(serialize_object, Float.parseFloat(read_field));
}
else if (class_name.equals("java.lang.Double")){
f=listFields.get(i);
f.set(serialize_object,Double.parseDouble(read_field));
}else if (class_name.equals("java.lang.Boolean")){
f=listFields.get(i);
f.set(serialize_object,Boolean.parseBoolean(read_field));
}
else if (class_name.equals("java.lang.String")){
f=listFields.get(i);
f.set(serialize_object, read_field);
}
else if (class_name.equals("java.lang.Byte")){
f=listFields.get(i);
f.set(serialize_object, read_field);
}
} else
listFields.get(i).set(serialize_object, null);
}
return serialize_object;
}
public String conversieTipuri(String class_name) {
String name="";
switch(class_name){
case "int" : name="java.lang.Integer";
break;
case "double": name="java.lang.Double";
break;
case "float": name="java.lang.Float";
break;
case "boolean": name="java.lang.Boolean";
break;
case "char": name="java.lang.Character";
break;
case "byte": name="java.lang.Byte";
break;
case "long": name="java.lang.Long";
break;
case "short": name="java.lang.Short";
break;
default: name=class_name;
break;
}
return name;
}
}
class Main1{
public static void main(String args[]){
try{
Angajat ang=new Angajat("Adi Vaduva", 20000, 21);
FileWriter f=new FileWriter("angajati1.ser");
BufferedWriter fos=new BufferedWriter(f);
// BufferedWriter fos=new BufferedWriter(new FileWriter("angajati1.ser"));
// ObjectOutputStream out=new ObjectOutputStream(fos);
//out.writeObject(ang);
MySerializer ms=new MySerializer();
ms.serializareObiect(fos, ang);
// BufferedReader fis=new BufferedReader(new FileReader("angajati1.ser"));
//ObjectInputStream in=new ObjectInputStream(fis);
f.close();
FileReader f1=new FileReader("angajati1.ser");
BufferedReader fis=new BufferedReader(f1);
ms.deserializareObject(fis);
f1.close();
}catch(IOException e){
System.out.println("eroare!");
}
catch(NoSuchMethodException e){
e.printStackTrace(); }
catch(Exception e){
e.printStackTrace();
}
}
}