java操作io对象流进行数据的读写-pg电子游戏官网

热度:12℃ 发布时间:2022-01-21 14:17:35

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

只有字节流没有字符流

.类必须实现serializable接口 给类加个序列化编号,给类定义一个标记,新的修改后的类还可以操作曾经序列化的对象 静态是不能被序列化的,序列化只能对堆中的进行序列化 ,不能对“方法区”中的进行序列化 不需要序列化的字段前加 transient

小例子:

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

package com.uwo9.test03; import .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;}@overridepublic 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 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 arraylist = (arraylist) 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();}} } }

到此这篇关于java操作io对象流进行数据的读写的文章就介绍到这了,更多相关java io流进行数据的读写内容请搜索软科小院以前的文章或继续浏览下面的相关文章希望大家以后多多支持软科小院!

网友评论
评论
更多编程技术
  • 编程技术推荐
更多
最新软件下载
网站地图