java - 对象序列化

标签 java serialization file-io

我一直在尝试做一个对象序列化、反序列化程序。当我直接在 FileInputStream 中给出文件名时,我的程序运行得很好,并且我能够成功地反序列化对象。但是当我尝试使用 FileDialog 时,序列化程序运行正常,但无法进行反序列化。我收到以下错误:

java.io.FileNotFoundException: nullnull (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at package1.Deserialisation.main(Deserialisation.java:22)
Exception in thread "main" java.lang.NullPointerException
    at package1.Deserialisation.main(Deserialisation.java:43) .

任何人都可以帮助我解决相同的问题,并简要解释其原因,以便不再重复错误。

以下是我的代码:

       package package1;
        public class Employee implements java.io.Serializable
        {
    private static final long serialVersionUID = 2L;

    String name;
    String address;
    int age;
    //int SSN;
    transient int SSN;    
    public void checkDetails()
    {
        System.out.println("The check details for the employee" + name + "are :");
    }

}

         Serialisation:
         package package1;
        import java.awt.FileDialog;
        import java.awt.Frame;
        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.io.ObjectOutputStream;

        public class Serialization 
         {
     public static void main(String[] args)
     {
          Employee emp =new Employee();
          emp.name="vidhya";
          emp.address="XYZ";
          emp.age=26;
          emp.SSN=123456;
          try 
          {   
            FileDialog fd =new FileDialog(new Frame(),"Save As...",FileDialog.SAVE);

            fd.setVisible(true);
            String filepath=new String (fd.getDirectory()+fd.getFile());
            FileOutputStream fileout =new FileOutputStream(filepath);
    ObjectOutputStream objout=new ObjectOutputStream(fileout);
    objout.writeObject(emp);
    fileout.close();
    objout.close();
    } 
          catch (IOException e2) {
        e2.printStackTrace();
        }
            }
       }


    Deserialisation :
         package package1;
        import java.awt.FileDialog;
        import java.awt.Frame;
        import java.io.FileInputStream;
        import java.io.IOException;
       import java.io.ObjectInputStream;
       public class Deserialisation 
       {
        static Employee emp=null;
    public static void main(String[] args) 
    {
      try {  
        FileDialog fd1 =new FileDialog(new Frame(),"Open...",FileDialog.LOAD);
        String filepath = new String(fd1.getDirectory() + fd1.getFile());
        FileInputStream filein =new FileInputStream(filepath);
        ObjectInputStream objin =new ObjectInputStream(filein);
        emp=(Employee) objin.readObject();
        objin.close();
        filein.close();
        } 

        catch (IOException e) 
        {
        e.printStackTrace();
        } 
        catch (ClassNotFoundException e1)
        {
            System.out.println("Employee class not found");
            e1.printStackTrace();
            return;       
        }

        System.out.println("Employee Name : " + emp.name);
        System.out.println("Address :" + emp.address);
        System.out.println("Age : " + emp.age);
        System.out.println("SSN :" + emp.SSN);
    }

}

最佳答案

String filepath = new String(fd1.getDirectory() + fd1.getFile());

这将创建一条“nullnull”路径,意味着这两个值均为 null。 尝试在上面的行之前使用 fd1.show();

编辑:正如 Viydha 所指出的,show() 已被弃用,应使用 setVisible()。

关于java - 对象序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4624966/

相关文章:

arrays - 在 node.js 和 socket.io 中发送数组时出现错误

c# - 将字典序列化到磁盘?

javascript - PHP 序列化失败并以半序列化格式存储

c# - 如何使用 System.IO.Directory.GetFiles 递归枚举文件+文件夹

c# - 如何知道上传的文件是否被更改?

c# - 从使用 List<T> 的 .Net 调用 Java Web 服务

java - struts2从2.3.16更新到2.3.32(修复S2-045)后,JSP文件无法解析某些Object的字段

file-io - 从文件读取后的命令行输入

java - 如果在这种情况下列表为空,我如何避免 java 中的空指针异常?

java - Mavericks OS X 上的几个应用程序请求安装 Java 6 SE 运行时,但已经有 Java 7