java序列化

阅读更多

今天有同事问我动态读写对象的问题,于是就想到了序列化。在此做下笔记,防止丢失被盗

 


java序列化_第1张图片
 

 

package org.vic.test.model;

import java.io.Serializable;
import java.util.List;

public class Class  implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4684355891224377925L;

	private String name;
	
	private List students;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List getStudents() {
		return students;
	}

	public void setStudents(List students) {
		this.students = students;
	}
	
}

 

 

package org.vic.test.model;

import java.io.Serializable;
import java.util.List;

public class Grade implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3162228744230892998L;

	private String name;
	
	private List classes;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List getClasses() {
		return classes;
	}

	public void setClasses(List classes) {
		this.classes = classes;
	}
	
}

 

package org.vic.test.model;

import java.io.Serializable;

public class Stu  implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3175234590284653474L;

	private String id;
	
	private String name;
	
	private int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	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;
	}
	
}

 

package org.vic.test.tester;

import java.io.Closeable;
import java.io.File;
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.util.ArrayList;
import java.util.List;

import org.vic.test.model.Grade;
import org.vic.test.model.Stu;

public class MyTester {

	private static final String path = "d:\\test.obj";

	public static void main(String[] args) throws FileNotFoundException,
			IOException, ClassNotFoundException {
		 Stu s0= new Stu();
		 s0.setId("1");
		 s0.setName("zhangsan");
		 s0.setAge(12);
		
		 Stu s1= new Stu();
		 s1.setId("2");
		 s1.setName("lisi");
		 s1.setAge(13);
		
		 Stu s2= new Stu();
		 s2.setId("3");
		 s2.setName("wangwu");
		 s2.setAge(14);
		
		
		 List list = new ArrayList<>();
		 list.add(s0);
		 list.add(s1);
		 list.add(s2);
		
		 org.vic.test.model.Class c = new org.vic.test.model.Class();
		 c.setStudents(list);
		 c.setName("class 1");
		 List classList = new ArrayList<>();
		 classList.add(c);
		
		
		
		 Grade g = new Grade();
		 g.setName("grade 5");
		 g.setClasses(classList);
		 
		 
		 write(g,null);

//		Grade g = read("");
//		String grade_name = g.getName();
//		System.out.println(grade_name);
//		List classes = g.getClasses();
//		for (org.vic.test.model.Class c : classes) {
//			String class_name = c.getName();
//			System.out.println(class_name);
//			List students = c.getStudents();
//			for (Stu s : students) {
//				int age = s.getAge();
//				String id = s.getId();
//				String name = s.getName();
//				System.out
//						.println("name=" + name + " age=" + age + " id=" + id);
//			}
//		}

	}

	public static void write(Grade g, String userPath)
			throws FileNotFoundException, IOException {
		File file;
		if (userPath == null || "".equals(userPath.trim())) {
			file = new File(path);
		} else {
			file = new File(userPath);
		}
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream fOut = new FileOutputStream(file);
		ObjectOutputStream out = new ObjectOutputStream(fOut);
		out.writeObject(g);
		out.flush();
		closeStreams(out);
	}

	public static Grade read(String userPath) throws IOException,
			ClassNotFoundException {
		File file;
		if (userPath == null || "".equals(userPath.trim())) {
			file = new File(path);
		} else {
			file = new File(userPath);
		}
		FileInputStream fIn = new FileInputStream(file);
		ObjectInputStream in = new ObjectInputStream(fIn);
		Grade g = (Grade) in.readObject();
		return g;
	}

	public static void closeStreams(Closeable... streams) throws IOException {
		if (streams != null) {
			for (Closeable c : streams) {
				if (c != null) {
					c.close();
				}
			}
		}
	}
}

 

  • java序列化_第2张图片
  • 大小: 55.3 KB
  • 查看图片附件

你可能感兴趣的:(java序列化)