亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Java操作IO對象流進行數據的讀寫

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

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

只有字節流沒有字符流

.類必須實現Serializable接口 給類加個序列化編號,給類定義一個標記,新的修改后的類還可以操作曾經序列化的對象 靜態是不能被序列化的,序列化只能對堆中的進行序列化 ,不能對“方法區”中的進行序列化 不需要序列化的字段前加 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;}@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<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();}} } }

到此這篇關于Java操作IO對象流進行數據的讀寫的文章就介紹到這了,更多相關Java IO流進行數據的讀寫內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 欧美精品久久一区二区三区 | 亚洲综合一区二区三区 | 亚洲精品aaa | 日韩高清一区二区 | 亚洲人和日本人jizz | 亚洲成a人片在线观看中文 亚洲成a人片在线观看中文动漫 | a毛片免费全部播放毛 | 精品成人免费播放国产片 | 亚洲精品一区二区三区五区 | 久久日本精品一区二区免费 | 色网站在线免费观看 | 中文精品爱久久久国产 | 国产在视频线精品视频二代 | 国产精品亚洲精品不卡 | 国产精品久久久久久久久久98 | 欧美精品亚洲精品日韩经典 | 亚洲欧美日韩精品在线 | 色中色综合 | 亚洲另类在线视频 | 久久好色 | 日韩黄色网址 | www视频在线免费观看 | 综合网视频 | 国产精品成人免费综合 | 韩国免费高清一级毛片性色 | 国产成人高清 | 国产精品第2页 | 2021久久精品永久免费 | 亚洲 欧美 中文字幕 | 天天影视欲香欲色成人网 | 99久久精品国产综合一区 | 欧美成人性色xxxx视频 | 国产一级淫片a免费播放口欧美 | 日本特黄特色大片免费视频观看 | a网站| 免费的黄网站 | 91原创视频 | 亚洲欧美日韩中文综合在线不卡 | 国产五月色婷婷综合 | 国产91av在线| 国产片网站|