student2.java

来自「介绍有关java的资料 课件 相当一本书籍 里面都是很基础的知识」· Java 代码 · 共 56 行

JAVA
56
字号
import java.io.*;
public class Student2 implements Serializable   
{
    int  number=1;
    String name;

    Student2(int number, String n1)
    {
       this.number=number;
       this.name=n1;
    }

    Student2()
    {
       this(0,"");
    }

    void save(String fname)
    {
       try
       {
         FileOutputStream  fout=new FileOutputStream(fname);
         ObjectOutputStream out=new ObjectOutputStream(fout);
         out.writeObject(this);   //写入对象
         out.close();
       }
       catch(FileNotFoundException fe){}
       catch(IOException ioe) {}
    }

    void display(String fname)
    {
      try
       {
         FileInputStream fin=new FileInputStream(fname);
         ObjectInputStream in=new ObjectInputStream(fin);
         Student2 u1=(Student2)in.readObject() ;      //读取对象
         System.out.println(u1.getClass().getName()+""+u1.getClass().getInterfaces()[0]);
         System.out.println(" "+u1.number+" "+u1.name);
         in.close();
       }
      catch(FileNotFoundException fe){}
      catch(IOException ioe){}
      catch(ClassNotFoundException ioe){}
    }

    public static void main(String arg[])
    {
       String    fname="student2.Obj"  ;
       Student2  s1=new Student2(1,"Wang")  ;
       s1.save(fname);
       s1.display(fname);
     }
 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?