A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王海生 中级黑马   /  2014-9-17 11:58  /  717 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ObjectStreamDemo {

        /**
         * 没有方法的接口通常叫标记接口。
         * @param args
         * @throws IOException
         * @throws ClassNotFoundException
         */
        public static void main(String[] args) throws IOException, ClassNotFoundException {
                // TODO 自动生成的方法存根

                writeobj();
                readobj();
        }
        public static void writeobj() throws IOException{
               
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));
                //写入一个对像
                oos.writeObject(new person("wanghai",21,"ch"));
                oos.close();
        }
        public static void readobj() throws IOException, ClassNotFoundException{
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"));
                person p = (person)ois.readObject();
                System.out.print(p);
                ois.close();
        }
}
class person implements Serializable{
        public static final long serialVersionUID = 42L;
        private String name;
        int age;
        static String country = "cn";
        person(String name,int age,String country){
                this.name = name;
                this.age = age;
                this.country = country;
        }
        public String toString(){
                return name+":"+age+":"+country;
        }
}

age和name的数据读存储在内存中,那么请问:如果想堆内存中的数据也不被序列化,那该怎么做?
还有country的数据"ch"能否写入到文件oos.txt中?

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马