Home Java javaTutorial java serialization object serializable instance of reading and writing data

java serialization object serializable instance of reading and writing data

Mar 18, 2017 am 11:35 AM

序列化对象

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();
 }
}
Copy after login
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");
   }
  }
 }
}
Copy after login

测试类:

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

更多java 序列化对象 serializable 读写数据的实例相关文章请关注PHP中文网!

相关文章:

Java序列化Serializable和Externalizable区别的示例代码

Java序列化之Serializable

php—Serializable接口

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles