Java操作IO对象流进行数据的读写

作者:幻影寒狼 时间:2021-08-25 03:33:49 

对象的读写
使用ObjectInputStream和ObjectOutputStream读写对象(序列化与反序列化)。

只有字节流没有字符流

  1. .类必须实现Serializable接口

  2. 给类加个序列化编号,给类定义一个标记,新的修改后的类还可以操作曾经序列化的对象

  3. 静态是不能被序列化的,序列化只能对堆中的进行序列化 ,不能对“方法区”中的进行序列化

  4. 不需要序列化的字段前加 transient

小例子:

先创建一个Dog对象并序列化:


package com.uwo9.test03;

import java.io.Serializable;

public class Dog implements Serializable {
private static final long serialVersionUID = 2809685095868158625L;
String name;
String color;
}

再创建一个Student对象并序列化:


package com.uwo9.test03;

import java.io.Serializable;

public class Student implements Serializable {
private static final long serialVersionUID = 9078616504949971001L;
static public  String schoolName;
private transient String name;
private transient int age;
private double score;
private Dog dog;
public Student() {
super();
}
public Student(String name, int age, double score, Dog dog) {
super();
this.name = name;
this.age = age;
this.score = score;
this.dog = dog;
}
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;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}

}

将数据写入对象流并存入文件


package com.uwo9.test03;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;

public class Test01 {

public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "大黄";
dog.color = "Yellow";
Student student1 = new Student("学生1", 18, 99,dog);
Student student2 = new Student("学生2", 19, 99,dog);
Student student3 = new Student("学生3", 20, 99,dog);
Student.schoolName = "某某大学";
File file = new File("E:/Temp/Test1.txt");
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(file));
//oos.writeObject(student);
ArrayList<Student> arrayList = new ArrayList<>();
Collections.addAll(arrayList, student1,student2,student3);
oos.writeObject(arrayList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

}

从指定文件中读取对象


package com.uwo9.test03;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class Test02 {

public static void main(String[] args) {
// 从指定的文件中读取对象
File file = new File("E:/Temp/Test1.txt");
ObjectInputStream ois=null;
try {
ois = new ObjectInputStream(new FileInputStream(file));
// 读取对象
// Student stu = (Student)ois.readObject();
// System.out.println("读取到的数据为:"+stu);
@SuppressWarnings("unchecked")
ArrayList<Student> arrayList = (ArrayList<Student>) ois.readObject();
for (Student student : arrayList) {
System.out.println(student);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

}

来源:https://blog.csdn.net/huanyinghanlang/article/details/78828347

标签:Java,IO流,数据读写
0
投稿

猜你喜欢

  • Android Studio中通过CMake使用NDK并编译自定义库和添加预编译库

    2023-06-16 10:31:35
  • IDEA与模拟器安装调试失败的处理方法:INSTALL_PARSE_FAILED_NO_CERTIFICATES

    2022-08-25 13:57:53
  • Spring RabbitMQ死信机制原理实例详解

    2022-04-29 18:52:18
  • Java深入讲解异常处理try catch的使用

    2023-11-04 13:00:32
  • spring中bean id相同引发故障的分析与解决

    2023-08-05 11:30:41
  • springboot手写一个自己的starter源码

    2021-07-31 10:18:14
  • 详解Spring框架入门

    2023-08-14 12:56:14
  • jdk8的datetime时间函数使用示例

    2021-07-03 16:42:37
  • Java毕业设计实战之医院心理咨询问诊系统的实现

    2022-07-04 19:02:21
  • 详解如何将Spring Boot应用跑在Docker容器中

    2023-04-25 08:08:58
  • 详解Java内存泄露的示例代码

    2023-06-08 03:34:51
  • Java转JSON串的几种方式

    2023-08-24 07:38:47
  • JAVA实现长连接(含心跳检测Demo)

    2023-08-05 09:51:13
  • Android切换至SurfaceView时闪屏(黑屏闪一下)以及黑屏移动问题的解决方法

    2023-07-21 10:51:45
  • java实现PPT转化为PDF

    2021-06-17 05:02:24
  • springboot拦截器Interceptor的使用,你都了解吗

    2023-01-01 21:53:40
  • intellij idea中spring boot properties文件不能自动提示问题解决

    2021-09-24 09:53:46
  • 详解Java并发包中线程池ThreadPoolExecutor

    2022-03-23 19:57:20
  • Java爬虫实现Jsoup利用dom方法遍历Document对象

    2023-06-15 07:52:36
  • Java使用pulsar-flink-connector读取pulsar catalog元数据代码剖析

    2023-11-05 17:25:41
  • asp之家 软件编程 m.aspxhome.com