欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 序列化对象 serializable 读写数据的实例

程序员文章站 2023-12-09 22:08:27
序列化对象: 复制代码 代码如下:package com.chen.seriaizable; import java.io.serializable;import ja...

序列化对象:

复制代码 代码如下:

package com.chen.seriaizable;

import java.io.serializable;
import java.util.list;

@suppresswarnings("serial")
public class student implements serializable
{
 private string name;

 private string id;

 private int age;

 private list<student> students;

 public string getname()
 {
  return name;
 }

 public void setname(string name)
 {
  this.name = name;
 }

 public string getid()
 {
  return id;
 }

 public void setid(string id)
 {
  this.id = id;
 }

 public int getage()
 {
  return age;
 }

 public void setage(int age)
 {
  this.age = age;
 }

 public list<student> getstudents()
 {
  return students;
 }

 public void setstudents(list<student> students)
 {
  this.students = students;
 }

 @override
 public string tostring()
 {
  stringbuffer stringbuffer = new stringbuffer();
  stringbuffer.append("id:" + this.id).append("\n");
  stringbuffer.append("name:" + this.name).append("\n");
  stringbuffer.append("age:" + this.age).append("\n");

  return stringbuffer.tostring();
 }
}


复制代码 代码如下:

package com.chen.seriaizable;

import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.objectinputstream;
import java.io.objectoutputstream;
import java.util.arraylist;
import java.util.list;

public class savestudent
{
 private student student;
 // 序列化文件保存位置
 private string path = "c:/student.serializable";

 public void writestudent()
 {
  list<student> students = new arraylist<student>();
  student = new student();

  student student1 = new student();
  student1.setage(1);
  student1.setid("1");
  student1.setname("张1");
  students.add(student1);

  student student2 = new student();
  student2.setage(2);
  student2.setid("2");
  student2.setname("张2");
  students.add(student2);

  student student3 = new student();
  student3.setage(3);
  student3.setid("3");
  student3.setname("张3");
  students.add(student3);

  student student4 = new student();
  student4.setage(4);
  student4.setid("4");
  student4.setname("张4");

  student student41 = new student();
  student41.setage(41);
  student41.setid("41");
  student41.setname("张41");
  list<student> students4 = new arraylist<student>();
  students4.add(student41);

  student4.setstudents(students4);

  students.add(student4);
 

  student.setage(500);
  student.setid("100");
  student.setname("张a000");
  student.setstudents(students);
  try
  {
   objectoutputstream objectoutputstream = new objectoutputstream(new fileoutputstream(path));

   objectoutputstream.writeobject(student);

   objectoutputstream.close();
  }
  catch (ioexception e)
  {
   // todo auto-generated catch block
   e.printstacktrace();
  }
  system.out.println("写完");
 }

 public void readstudent()
 {
  try
  {
   objectinputstream objectinputstream = new objectinputstream(new fileinputstream(path));

   student = (student) objectinputstream.readobject();

   system.out.println(student.getage());

   objectinputstream.close();
  }
  catch (exception e)
  {
   e.printstacktrace();
  }
  system.out.println("读完");
 }

 @override
 public string tostring()
 {
  readstudent();
  stringbuffer stringbuffer = new stringbuffer(100);

  studenttostring(stringbuffer, student);

  return stringbuffer.tostring();
 }

 public void studenttostring(stringbuffer stringbuffer, student studentobj)
 {
  if (student != null)
  {
   stringbuffer.append("id:" + studentobj.tostring()).append("\n");
   if (studentobj.getstudents() != null)
   {
    stringbuffer.append("\n[\n");
    for (student bean : studentobj.getstudents())
    {
     studenttostring(stringbuffer, bean);
    }
    stringbuffer.append("\n]\n");
   }
  }
 }

}


测试类:

复制代码 代码如下:

package com.chen.seriaizable;

public class test
{
 /**
  * @param args
  */
 public static void main(string[] args)
 {
  savestudent savestudent = new savestudent();
  // 1 先执行写入文件
//  savestudent.writestudent();
  // 2 再读取
  system.out.println(savestudent);
  system.out.println("读完");
 }

}