Cómo deserializar el objeto actual

Jeewantha Lahiru:

He creado un programa bankStatement y hay dos clases como principal y FinancialManager .En este programa puedo retirar, depósito y ver el balance de la cuenta corriente. este es mi FinancialManager clase

import java.io.*;
import java.util.Vector;

public class FinancialManager implements Serializable {
    private double balance1;
    private Vector<String> statement1;
    public FinancialManager(){
        balance1=0;
        statement1 = new Vector<String>();
    }
    public void deposit(double value){
        balance1 = balance1+value;
        String st = "Deposit  "+String.valueOf(value);
        statement1.add(st);
    }
    public void withdraw(double value){
        if(value<balance1){
            balance1 = balance1 - value;
            String st = "Withdraw "+String.valueOf(value);
            statement1.add(st);
        }else{
            String st = "Withdraw 0.0";
            statement1.add(st);
        }
    }
    public String balance(){
        return String.valueOf(balance1);
    }
    public void statement(){
        String[] array = statement1.toArray(new String[statement1.size()]);
        for(int i=0;i<array.length;i++){
            System.out.println(array[i]);
        }
    }

    public void save(String fileName) throws IOException {
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(fileName))) {
            // write "this" - the current object - to the file
            objectOutputStream.writeObject(this);
        }
    }

}

Y mi clase principal se encuentra por debajo

public class Main {
    public static final Scanner scan = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        FinancialManager fm = new FinancialManager();
        fm.deposit(25.00);
        fm.withdraw(12.00);
        fm.deposit(10.00);
        fm.deposit(5.00);
        fm.withdraw(8.00);
        System.out.println("The current balance is "+fm.balance());
        fm.statement();
        fm.save("text.txt");

        FinancialManager anotherfm = new FinancialManager();

    }
}

Podría crear la serialización método en mi FinancialManager usar este keyword.but no sé cómo crear Deserialización method.how puedo hacer eso? Por favor, ayúdame con this.The método debería ser así en FinancialManager clase.

public void load(String filename){
   //Write your code here
}

Necesidad de utilizar este método en la clase principal como esto

FinancialManager anotherFM = new FinancialManager(); 
anotherFM.load(laccountl");
anotherFM.statement();

Tom Hawtin - tackline:

Desrialisation crea el objeto. El objeto no debe existir ya. Por lo tanto hacer en static, al igual que un método de creación (tal como List.of).

Sin embargo, la codificación segura Directrices para Java SE dice:

Nota: Deserialización de datos no confiables es inherentemente peligroso y debe ser evitado.

No tocaría serialización Java.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=359806&siteId=1
Recomendado
Clasificación