av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java操作IO對(duì)象流進(jìn)行數(shù)據(jù)的讀寫

瀏覽:3日期:2022-08-10 16:22:21

對(duì)象的讀寫使用ObjectInputStream和ObjectOutputStream讀寫對(duì)象(序列化與反序列化)。

只有字節(jié)流沒有字符流

.類必須實(shí)現(xiàn)Serializable接口 給類加個(gè)序列化編號(hào),給類定義一個(gè)標(biāo)記,新的修改后的類還可以操作曾經(jīng)序列化的對(duì)象 靜態(tài)是不能被序列化的,序列化只能對(duì)堆中的進(jìn)行序列化 ,不能對(duì)“方法區(qū)”中的進(jìn)行序列化 不需要序列化的字段前加 transient

小例子:

先創(chuàng)建一個(gè)Dog對(duì)象并序列化:

package com.uwo9.test03; import java.io.Serializable; public class Dog implements Serializable {private static final long serialVersionUID = 2809685095868158625L;String name;String color;}

再創(chuàng)建一個(gè)Student對(duì)象并序列化:

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 + ']';} }

將數(shù)據(jù)寫入對(duì)象流并存入文件

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('學(xué)生1', 18, 99,dog);Student student2 = new Student('學(xué)生2', 19, 99,dog);Student student3 = new Student('學(xué)生3', 20, 99,dog);Student.schoolName = '某某大學(xué)';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();}}} }

從指定文件中讀取對(duì)象

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) {// 從指定的文件中讀取對(duì)象File file = new File('E:/Temp/Test1.txt');ObjectInputStream ois=null;try {ois = new ObjectInputStream(new FileInputStream(file));// 讀取對(duì)象// Student stu = (Student)ois.readObject();// System.out.println('讀取到的數(shù)據(jù)為:'+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();}} } }

到此這篇關(guān)于Java操作IO對(duì)象流進(jìn)行數(shù)據(jù)的讀寫的文章就介紹到這了,更多相關(guān)Java IO流進(jìn)行數(shù)據(jù)的讀寫內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 天天操天天操天天操 | 狠狠做深爱婷婷综合一区 | 欧美激情啪啪 | 国产精品久久久久久久午夜 | 激情婷婷综合 | 国产伦精品一区二区三区视频我 | 欧美18免费视频 | 中国a一片一级一片 | 久久精品视频网站 | 80日本xxxxxxxxx96 国产成人在线免费视频 | 午夜成人在线视频 | 美女国产精品 | 国产精品免费在线播放 | 日本视频免费观看 | 国产性色av| 国产中文在线 | 国产欧美日韩一区 | 国产精品视频一区二区三区 | 国产精品99久久久久久久久久久久 | 国产三级做爰高清在线 | 精品国产一区二 | 97人人插 | 国产中文字幕在线播放 | 国产色在线 | 精品视频在线免费观看 | 亚洲永久精品视频 | av网页在线观看 | 狠狠躁夜夜躁人爽 | 国产成人在线观看免费网站 | 日韩中文字幕在线观看 | 色网站女女 | 九九热在线播放 | 色在线视频 | 国产精品久久久久久久免费看 | 在线一区 | 成人不卡视频 | 日韩欧美三区 | 清清草视频 | 一区二区欧美日韩 | 欧美在线观看一区 | av日韩精品|