码迷,mamicode.com
首页 > 其他好文 > 详细

ObjectInputStreamTest 对象类型输入输出流

时间:2016-08-15 19:08:50      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

package IOliu;

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 ObjectInputStreamTest {

    public static void main(String[] args) {
        File file = null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        User user = null;
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            //保存对象数据到内存中
            file = new File("D:\\2017.txt");
            fos = new FileOutputStream(file);
            oos = new ObjectOutputStream(fos);
            user = new User("Rose",24,"892524460@qq.com","170");
            oos.writeObject(user);
            //从内存中获取对象数据
            fis = new FileInputStream(file);
            ois = new ObjectInputStream(fis);
            try {
                User user1 = (User) ois.readObject();
                System.out.println(user1.getName()+" "+user1.getAge()+" "+user1.getMail()+" "+user.getHeight());
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                oos.flush();
                oos.close();
                ois.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
package IOliu;

import java.io.Serializable;

//加接口 对象序列化implements
public class User implements Serializable{
    private String name;
    private int age;
    private String mail;
    private String height;
    
    public User() {
        super();
    }
    public User(String name, int age, String mail, String height) {
        super();
        this.name = name;
        this.age = age;
        this.mail = mail;
        this.height = height;
    }
    public String getMail() {
        return mail;
    }
    public void setMail(String mail) {
        this.mail = mail;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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;
    }

}

 

ObjectInputStreamTest 对象类型输入输出流

标签:

原文地址:http://www.cnblogs.com/xiaolei121/p/5773897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!