Java串行化读写文件(多实例)

题目

创建可串行化的WaterResource类,有两个属性:国家名、人均水资源量。自定义文件data.dat,同时在Java程序中创建5个WaterResource类的实例,使用ObjectOutputStream类将这5个实例写入到data.dat文件中,再使用ObjectInputStream类将其读入并输出显示

分析

java.io.Serializable 接口是一个标记接口(不含有数据和方法),String和所有的原始数据类型的包装器类都默认实现了该接口

ObjectOutputStream 类用来串行化对象为OutputStream,类字段如果是引用,对应的引用对象也需要序列化

ObjectInputStream 类用来反序列化先前串行化的原始数据和对象,重构对象

思路

首先,创建包含两个属性的WaterResource类:

package serialize;

import java.io.Serializable;

public class WaterResource implements Serializable{
	
	/**
	 * default ID
	 */
	private static final long serialVersionUID = 1L;

	private String countryName;
	 
    private int averageWater;

	public String getCountryName() {
		return countryName;
	}

	public void setCountryName(String countryName) {
		this.countryName = countryName;
	}

	public int getAverageWater() {
		return averageWater;
	}

	public void setAverageWater(int averageWater) {
		this.averageWater = averageWater;
	}

	@Override
	public String toString() {
		return "WaterResource [countryName=" + countryName + ", averageWater=" + averageWater + "]";
	}
 
}

其次,创建一个实现功能的类WRSerializable:

package serialize;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class WRSerializable {
	 
    /**
     *将对象序列化到磁盘文件中
     */
 
    public static void writeObject(Object ob[]) throws Exception{
 
       File f=new File("e:"+"data.dat");
 
       if(f.exists()){
           f.delete();
       }
 
       FileOutputStream fos=new FileOutputStream(f);
 
       ObjectOutputStream oos=new ObjectOutputStream(fos);
 
       for (Object o:ob) {
    	   oos.writeObject(o);
       }
       
       fos.close();
       oos.close();
    }
 
    /**
     *反序列化,将磁盘文件转化为对象 
     */
 
    public static WaterResource readObject(File f) throws Exception{
 
		InputStream is=new FileInputStream(f);
		
		@SuppressWarnings("resource")
		ObjectInputStream ois=new ObjectInputStream(is);
		 
		return (WaterResource)ois.readObject();
	}
 
    
    public static void main(String[] args) throws Exception{
 
       /*****************将对象序列化***************/
 
    	WaterResource wr1 = new WaterResource();
    	WaterResource wr2 = new WaterResource();
    	WaterResource wr3 = new WaterResource();
    	WaterResource wr4 = new WaterResource();
    	WaterResource wr5 = new WaterResource();
 
    	wr1.setCountryName("country1");
    	wr1.setAverageWater(1000);
    	wr2.setCountryName("country2");
    	wr2.setAverageWater(2000);
       	wr3.setCountryName("country3");
    	wr3.setAverageWater(3000);
    	wr4.setCountryName("country4");
    	wr4.setAverageWater(4000);
       	wr5.setCountryName("country5");
    	wr5.setAverageWater(5000);
    	
    	WaterResource wr_arr[] = new WaterResource[]{wr1,wr2,wr3,wr4,wr5};
    	WRSerializable.writeObject(wr_arr);
    	
    	System.out.println("WRITE TO data.dat:");
        System.out.println(wr1);
        System.out.println(wr2);
        System.out.println(wr3);
        System.out.println(wr4);
        System.out.println(wr5);
    	
       /*****************将对象序反列化***************/
        
        FileInputStream fis = new FileInputStream("e:"+"data.dat");
        ObjectInputStream osi = new ObjectInputStream(fis);
        
        System.out.println("READ FROM data.dat:");
    	try {
            while(true) {
            	WaterResource wr = (WaterResource) osi.readObject();
                System.out.println(wr);
            }
        }
        catch(EOFException e) {
            
        }
        finally {
            osi.close();
        }
    	
    }
}

结果展示

-Java串行化读写文件(多实例)-

♥ 喜 欢 请 点 赞 哟 ♥
(●ˇ∀ˇ●)
未分类>
匿名

发表评论

匿名网友