java 序列化與還原序列化

來源:互聯網
上載者:User

標籤:dex   otf   sep   注意   rac   log   dir   style   parent   

package open_exe;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class xuliehua {    public void xulie(){        Student st=new Student();        st.setName("測試1");        st.setAge(18);        st.setSex("男");        st.setHeight("168cm");        File file= new File("C:"+File.separator+"file1"+File.separator+"student.txt");           try            {                if(!file.getParentFile().exists()){//判斷檔案夾在不在                    file.getParentFile().mkdirs();//檔案夾不在 建立檔案夾              }if(!file.exists()){//判斷txt檔案在不在                   file.createNewFile();//txt不在就建立一個txt              }          }            catch(IOException e)            {             e.printStackTrace();            }            try            {             //Student對象序列化過程             FileOutputStream fos = new FileOutputStream(file);             ObjectOutputStream oos = new ObjectOutputStream(fos);//序列化           oos.writeObject(st);//寫入txt檔案           oos.flush();             oos.close();             fos.close();            System.out.println("完成系列化!");          }              catch (IOException e)            {             e.printStackTrace();           System.out.println("系列化失敗!");          }     }public void fan(){    File file = new File("C:\\file\\student.txt");         try        {         //Student對象序列化過程            FileInputStream fis = new FileInputStream(file);             ObjectInputStream ois = new ObjectInputStream(fis);//還原序列化           Student st1 = (Student) ois.readObject();           System.out.println("name = " + st1.getName());             System.out.println("sex = " + st1.getSex());               ois.close();             fis.close();           System.out.println("還原序列化成功!");      }catch(ClassNotFoundException e)        {             e.printStackTrace();           System.out.println("還原序列化失敗!");          }       catch (IOException e)        {         e.printStackTrace();        } }    public static void main(String[] args){        // TODO 自動產生的方法存根        xuliehua n=new xuliehua();        n.xulie();               }}

 

package open_exe;import java.io.Serializable;public class Student implements Serializable{//注意這兒需要實現serializable介面    private static final long serialVersionUID = 7146811040856818256L;    private String name;    private String sex;    private int age;    private String height;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getHeight() {        return height;    }    public void setHeight(String height) {        this.height = height;    }    }

 

java 序列化與還原序列化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.