程序没有错误时,序列化对象文件为空

时间:2016-10-16 05:28:49

标签: java serialization

在成功运行以下代码后,我不确定为什么文件BankAccount.ser为空。 BankAccount.ser文件是类路径资源。成功运行SuccessfulSerializationTestDriver后,BankAccount.ser在磁盘上为零字节且没有内容。

public class SuccessfulSerializationTestDriver {

    public static void main(String[] args) {

        long accountNumber=12033456;
        String bankName="SBI";
        String branch="NOIDA";

        SerializableBankAccount sBankAccount = new SerializableBankAccount();

        sBankAccount.setAccountNumber(accountNumber);
        sBankAccount.setBankName(bankName);
        sBankAccount.setBranch(branch);

        try(FileOutputStream fileOut =new FileOutputStream("BankAccount.ser")){
            ObjectOutputStream out= new ObjectOutputStream(fileOut);
            out.writeObject(sBankAccount);
            out.flush();
            out.close();
            System.out.println("Bank Account is successfully serialized");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }   

    }

}

Serializable类是,

public class SerializableBankAccount implements Serializable {

    private static final long serialVersionUID = 1L;
    private long accountNumber;
    private String bankName;
    private String branch;

    public long getAccountNumber() {
        return accountNumber;
    }
    public void setAccountNumber(long accountNumber) {
        this.accountNumber = accountNumber;
    }
    public String getBankName() {
        return bankName;
    }
    public void setBankName(String bankName) {
        this.bankName = bankName;
    }
    public String getBranch() {
        return branch;
    }
    public void setBranch(String branch) {
        this.branch = branch;
    }

    @Override
    public String toString(){
      return accountNumber+","+bankName+","+branch;
    }
}
编辑:我写了反序列化器,我成功地获得了对象 - 所以它似乎只是一个可见性问题。不知何故,文件显示为零字节。

public class SuccessfulDeSerializationTestDriver {

    public static void main(String[] args) {
        SerializableBankAccount sBankAccount = null;

        try(FileInputStream fileIn =new FileInputStream("BankAccount.ser")){
            ObjectInputStream inStream= new ObjectInputStream(fileIn);
            sBankAccount= (SerializableBankAccount) inStream.readObject();
            inStream.close();
            System.out.println("Successfully Deserialized Object is "+sBankAccount);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

}

成功反序列化的对象是12033456,SBI,NOIDA

2 个答案:

答案 0 :(得分:1)

我无法重现您遇到的问题。当我运行您的代码时,会创建BankAccount.ser文件并且不为空。事实上,我写了一个反序列化测试,看看我是否可以通过读取文件来获取对象,并且它可以正常工作。

如果需要,可以使用反序列化类:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class DeserializationTestDriver {

    public static void main(String[] args) {
        try(FileInputStream fileInput =new FileInputStream("BankAccount.ser")){
            ObjectInputStream input = new ObjectInputStream(fileInput);
            SerializableBankAccount sBankAccount = (SerializableBankAccount) input.readObject();
            input.close();
            System.out.println("Bank Account is successfully deserialized: "+sBankAccount.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }   
    }

}

我还在toString添加了SerializableBankAccount方法:

@Override
public String toString() {
    return "SerializableBankAccount [accountNumber=" + accountNumber
            + ", bankName=" + bankName + ", branch=" + branch + "]";
}

运行序列化代码然后运行上面的反序列化后,我得到了这个输出:

银行帐户已成功反序列化:SerializableBankAccount [accountNumber = 12033456,bankName = SBI,branch = NOIDA]

很明显代码很好,这意味着它必须与环境有关。我建议您检查是否正在运行具有正确权限,权限等的程序。您的代码外部的某些内容似乎阻止您写入该文件。无论是或者您正在查看错误的文件,请验证您是否具有正确的路径并检查文件创建和修改日期。

答案 1 :(得分:1)

如果您正在查看的文件是零字节,但它成功反序列化,则听起来就像是在其他地方创建了文件。也许在为开始创建文件名时明确指定路径。大小为0的文件可能来自较旧的运行 - 删除磁盘上的文件,并查看它是否再次创建。