I want to increase a number with serialization but I can't do it.
[THIS IS THE COMPLETE CODE]
System Class
package ejemplo;
class Sistema{
public static void main(String[] args) {
/*Num numero = new Num(0);
Serializadora serializadora = new Serializadora();
serializadora.escribiendo(numero);*/
Indice indice = null;
Serializadora serializadora = new Serializadora();
numero = (Num) serializadora.leerObjeto("objeto.dat");
System.out.println(numero.getF());
}
}
Class Number
package ejemplo;
import java.io.Serializable;
import javax.swing.text.StyledEditorKit.ForegroundAction;
public class Num implements Serializable {
private int f;
public int getF() {
return f;
}
public void setF(int f) {
this.f=f;
}
public void postSerializacion(){
if (f<20){
f++;
}
else {
f=0;
}
System.out.println("este es el numero "+f);
}
public Num(int f) {
super();
this.f = f;
System.out.println(f);
}
}
Serializer class
package ejemplo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Serializadora {
private ObjectInputStream lee;
private ObjectOutputStream escribe;
public void escribiendo(Object objeto) {
try {
escribe = new ObjectOutputStream(new FileOutputStream("objeto.dat"));
escribe.writeObject(objeto);
} catch (FileNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object leerObjeto(String nombredelarchivo) {
Object retorno = null;
try {
lee = new ObjectInputStream(new FileInputStream(nombredelarchivo));
retorno = lee.readObject();
if(retorno instanceof Num){
((Num)retorno).postSerializacion();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retorno;
}
}
What I want to do is if f is less than 20, increase the serialized number by one by replacing f until reaching f=20 and at the end print "this is the number {here the number}"
I recommend you create a method in the Num something like this:
And modify your method: