如何反序列化当前对象
How to Deserialize the current object
我创建了一个 bankStatement 程序,其中有两个 class,例如 Main 和 FinancialManager。在这个程序中我可以取款、存款并查看当前账户余额。
这是我的 FinancialManager class
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);
}
}
}
我的主要 class 在下面
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();
}
}
我可以在我的 FinancialManager 中创建 Serialization 方法 keyword.but 我不知道如何创建 反序列化 method.how 我可以这样做吗?
请帮助我 this.The 方法在 FinancialManager class.
中应该是这样的
public void load(String filename){
//Write your code here
}
需要在 Main 中使用这个方法 class 像这样
FinancialManager anotherFM = new FinancialManager();
anotherFM.load(laccountl");
anotherFM.statement();
反序列化创建对象。该对象不应该已经存在。因此make in static
,与创建方法相同(如List.of
)。
然而,Secure Coding Guidelines for Java SE 表示:
Note: Deserialization of untrusted data is inherently dangerous and
should be avoided.
我不会碰Java序列化。
我创建了一个 bankStatement 程序,其中有两个 class,例如 Main 和 FinancialManager。在这个程序中我可以取款、存款并查看当前账户余额。 这是我的 FinancialManager class
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);
}
}
}
我的主要 class 在下面
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();
}
}
我可以在我的 FinancialManager 中创建 Serialization 方法 keyword.but 我不知道如何创建 反序列化 method.how 我可以这样做吗? 请帮助我 this.The 方法在 FinancialManager class.
中应该是这样的public void load(String filename){
//Write your code here
}
需要在 Main 中使用这个方法 class 像这样
FinancialManager anotherFM = new FinancialManager();
anotherFM.load(laccountl");
anotherFM.statement();
反序列化创建对象。该对象不应该已经存在。因此make in static
,与创建方法相同(如List.of
)。
然而,Secure Coding Guidelines for Java SE 表示:
Note: Deserialization of untrusted data is inherently dangerous and should be avoided.
我不会碰Java序列化。