zoukankan      html  css  js  c++  java
  • Java基础之对象的序列化(持久化)操作对象ObjectInputStream/ObjectOutputStream

    import java.io.*;

    class ObjectInputStreamDemo
    {
        public static void main(String[] args) throws Exception
        {    
            String fileName = "obj.txt";
            writer(fileName,new Person("陈晓明",28));
            Person person = reader(fileName);
            
            System.out.println(person.toString());
        }
        
        public static Person reader(String fileName) throws Exception
        {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(fileName)));
            Person person = (Person)ois.readObject();        
            ois.close();
            
            return person;
        }
        
        public static void writer(String fileName,Person person)  throws Exception
        {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(fileName)));
            oos.writeObject(person);        
            oos.close();
        }
    }

    class Person implements Serializable
    {
        public static final long serialVersionUID = 42L;

        String name;
        int age;
        
        public Person(String name,int age)
        {
            this.name = name;
            this.age = age;
        }
        
        public String toString()
        {
            return (this.name + "::" + this.age);
        }
    }
  • 相关阅读:
    八皇后问题
    窃贼问题
    汉诺塔算法
    HDOJ(HDU) 1570 A C
    HttpClient4.2 Fluent API学习
    CUDA编程(六)进一步并行
    动态规划-迷宫-百度之星-Labyrinth
    hdu 5288||2015多校联合第一场1001题
    [单调队列] hdu 3415 Max Sum of Max-K-sub-sequence
    java 内存数据存储
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2889766.html
Copyright © 2011-2022 走看看