Java操作IO對象流進行數(shù)據(jù)的讀寫
對象的讀寫
使用ObjectInputStream和ObjectOutputStream讀寫對象(序列化與反序列化)。
只有字節(jié)流沒有字符流
- .類必須實現(xiàn)Serializable接口
- 給類加個序列化編號,給類定義一個標記,新的修改后的類還可以操作曾經序列化的對象
- 靜態(tài)是不能被序列化的,序列化只能對堆中的進行序列化 ,不能對“方法區(qū)”中的進行序列化
- 不需要序列化的字段前加 transient
小例子:
先創(chuàng)建一個Dog對象并序列化:
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)建一個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 + "]"; } }
將數(shù)據(jù)寫入對象流并存入文件
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("讀取到的數(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(); } } } }
到此這篇關于Java操作IO對象流進行數(shù)據(jù)的讀寫的文章就介紹到這了,更多相關Java IO流進行數(shù)據(jù)的讀寫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Intellj Idea中的maven工程Java文件顏色不對,未被識別的解決
這篇文章主要介紹了Intellj Idea中的maven工程Java文件顏色不對,未被識別的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08java并發(fā)學習-CountDownLatch實現(xiàn)原理全面講解
這篇文章主要介紹了java并發(fā)學習-CountDownLatch實現(xiàn)原理全面講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02使用jaxp進行dom解析_動力節(jié)點Java學院整理
這篇文章主要介紹了使用jaxp進行dom解析的相關資料,需要的朋友可以參考下2017-08-08java發(fā)送heartbeat心跳包(byte轉16進制)
這篇文章主要介紹了java發(fā)送heartbeat心跳包(byte轉16進制),需要的朋友可以參考下2014-05-05SpringCloud微服務續(xù)約實現(xiàn)源碼分析詳解
這篇文章主要介紹了SpringCloud微服務續(xù)約實現(xiàn)源碼分析,服務續(xù)期和服務注冊非常相似,服務注冊在Eureka?Client程序啟動之后開啟,并同時開啟服務續(xù)期的定時任務2022-11-11Java基礎之多線程方法狀態(tài)和創(chuàng)建方法
Java中可以通過Thread類和Runnable接口來創(chuàng)建多個線程,下面這篇文章主要給大家介紹了關于Java基礎之多線程方法狀態(tài)和創(chuàng)建方法的相關資料,需要的朋友可以參考下2021-09-09