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

y569705158

y569705158

2016-02-19 11:04

下面,图老师小编带您去了解一下java 序列化对象 serializable 读写数据的实例,生活就是不断的发现新事物,get新技能~

序列化对象:

代码如下:

package com.chen.seriaizable;

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

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

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

 public ListStudent getStudents()
 {
  return students;
 }

 public void setStudents(ListStudent 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()
 {
  ListStudent students = new ArrayListStudent();
  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");
  ListStudent students4 = new ArrayListStudent();
  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;

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

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("读完");
 }

}

展开更多 50%)
分享

猜你喜欢

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

编程语言 网络编程
java 序列化对象 serializable 读写数据的实例

深入理解Java对象的序列化与反序列化的应用

编程语言 网络编程
深入理解Java对象的序列化与反序列化的应用

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

利用序列化进行文件读写

编程语言 网络编程
利用序列化进行文件读写

通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方

Web开发
通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

基于序列化存取实现java对象深度克隆的方法详解

编程语言 网络编程
基于序列化存取实现java对象深度克隆的方法详解

序列化FastReport

编程语言 网络编程
序列化FastReport

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

如何使你的PPT更易于阅读

如何使你的PPT更易于阅读

Excel 2024中如何插入联机图片

Excel 2024中如何插入联机图片
下拉加载更多内容 ↓