日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

IO-5(InputStreamReader、OutputStreamWriter、序列化流、反序列化流、Serializable、transient)

發(fā)布時(shí)間:2024/10/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IO-5(InputStreamReader、OutputStreamWriter、序列化流、反序列化流、Serializable、transient) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.InputStreamReader

字節(jié)流通向字符的橋梁

所有已實(shí)現(xiàn)的接口:

Closeable, Readable

直接已知子類:

FileReader

將字節(jié)輸入流轉(zhuǎn)換為字符輸入流

使用指定的字符編碼表(可顯式指定也可接受默認(rèn)),讀取字節(jié)并將其解碼為字符

1.1構(gòu)造函數(shù)

InputStreamReader(InputStream in):創(chuàng)建一個(gè)使用默認(rèn)字符集的InputStreamReader

InputStreamReader(InputStream in,Charset chas):創(chuàng)建一個(gè)使用指定字符集的InputStreamReader

1.2 方法

int read():讀取單個(gè)字符

int read(char[] chs,int index,int len):?將字符讀入數(shù)組中的某一部分

1.3 使用步驟

import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;public class Test{public static void main(String[] args) throws IOException {//關(guān)聯(lián)文件和字節(jié)輸入流對(duì)象InputStream in=new FileInputStream("a.txt");//轉(zhuǎn)換流對(duì)象,字節(jié)--->字符InputStreamReader isr=new InputStreamReader(in,"GBK");//utf-8時(shí)是問(wèn)號(hào),不寫也可正常輸出int ch=0;while((ch=isr.read())!=-1)System.out.print((char)ch);isr.close();in.close();} }

運(yùn)行結(jié)果:

2.OutputStreamWriter

字符流通向字節(jié)流的橋梁。

所有已實(shí)現(xiàn)的接口:

Closeable, Flushable, Appendable

直接已知子類:

FileWriter

作用就是利用字節(jié)流作為底層輸出流然后構(gòu)建字符輸出流,字符輸出流輸出字符到流中,然后通過(guò)指定的字符集把流中的字符編碼成字節(jié)輸出到字節(jié)流中,其作用就是一個(gè)橋梁,使得雙方鏈接起來(lái)。

2.1構(gòu)造函數(shù)

OutputStreamWriter(OutputStream out):創(chuàng)建使用默認(rèn)字符編碼的 OutputStreamWriter。

OutputStreamWriter(OutputStream out,Charset cs):創(chuàng)建使用給定字符集的 OutputStreamWriter。

2.2 方法

void write(int c):寫入單個(gè)字符

void write(char[] chs,int index,int len):寫入字符數(shù)組的某一部分

2.3使用步驟

import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter;public class Test{public static void main(String[] args) throws IOException {OutputStream out=new FileOutputStream("a.txt");OutputStreamWriter osw=new OutputStreamWriter(out,"GBK");osw.write("我在呢!");osw.close();out.close();} }

運(yùn)行結(jié)果:

2.4總結(jié)

父類:OutputStreanWriter、InputStreamReader,字符和字節(jié)之間的橋梁,源流:字節(jié)流+編碼表

子類:FileWriter、FileReader,操作文件的便捷類,使用默認(rèn)的編碼表時(shí),可不用父類。

字節(jié)->字符,讀,輸入流,InputStreamReader

字符->字節(jié),寫,輸出流,OutputStreamWriter

3.ObjectOutputStream

序列化流

所有已實(shí)現(xiàn)的接口:

Closeable, DataOutput, Flushable, ObjectOutput, ObjectStreamConstants

用于向文件中寫入對(duì)象,將 Java 對(duì)象的基本數(shù)據(jù)類型和圖形寫入 OutputStream。可

可以使用 ObjectInputStream 讀取(重構(gòu))對(duì)象。

只能將支持 java.io.Serializable 接口的對(duì)象寫入流中,若未實(shí)現(xiàn),會(huì)出現(xiàn)NotSerializableException

3.1構(gòu)造方法

ObjectOutpputStream(OutputStream out):創(chuàng)建寫入指定 OutputStream 的 ObjectOutputStream

3.2 方法

void writeObject(Object obj):將指定的對(duì)象寫入 ObjectOutputStream

void writeInt(int i):寫入int值

3.3 將一個(gè)對(duì)象存儲(chǔ)到持久化設(shè)備上的步驟

  • 明確存儲(chǔ)對(duì)象的文件
  • 給操作文件對(duì)象加入寫入對(duì)象的功能
  • 調(diào)用寫入對(duì)象的方法
  • 關(guān)閉資源
import java.io.Serializable;public class Person implements Serializable{/*** */private static final long serialVersionUID = 1L;String name;int age;public Person(String name,int age) {this.name=name;this.age=age;}public String toString() {return "name:"+this.name+",age:"+this.age;} } import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream;public class Test{public static void main(String[] args) throws IOException {FileOutputStream fos=new FileOutputStream("a.txt");ObjectOutputStream oos=new ObjectOutputStream(fos);oos.writeObject(new Person("zhangsan",30));oos.close();fos.close();} }

運(yùn)行結(jié)果:

4.ObjectInputStream

反序列化流,對(duì)以前使用 ObjectOutputStream 寫入的基本數(shù)據(jù)和對(duì)象進(jìn)行反序列化。

所有已實(shí)現(xiàn)的接口:

Closeable, DataInput, ObjectInput, ObjectStreamConstants

用于從流中讀取對(duì)象

ObjectOutputStream 和 ObjectInputStream 分別與 FileOutputStream 和 FileInputStream 一起使用時(shí),可以為應(yīng)用程序提供對(duì)對(duì)象圖形的持久存儲(chǔ)。

只有支持 java.io.Serializable java.io.Externalizable 接口的對(duì)象才能從流讀取。

4.1構(gòu)造函數(shù)

ObjectInputStream(FileInputStream in):創(chuàng)建從指定 InputStream 讀取的 ObjectInputStream。

4.2方法

Object readObject():從 ObjectInputStream 讀取對(duì)象。

int readInt():?讀取一個(gè) 32 位的 int 值。

4.3 對(duì)象的反序列化步驟

import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream;public class Test{public static void main(String[] args) throws IOException, ClassNotFoundException {FileInputStream fis=new FileInputStream("a.txt");ObjectInputStream ois=new ObjectInputStream(fis);Person p=(Person)ois.readObject();System.out.println(p.toString());ois.close();fis.close();} }

運(yùn)行結(jié)果:

5.Serializable接口

java.io.Serializable

類通過(guò)實(shí)現(xiàn) java.io.Serializable 接口以啟用其序列化功能。

未實(shí)現(xiàn)此接口的類將無(wú)法使其任何狀態(tài)序列化或反序列化

可序列化類的所有子類型本身都是可序列化的。

序列化接口沒(méi)有方法或字段,僅用于標(biāo)識(shí)可序列化的語(yǔ)義。

5.1 常見問(wèn)題

  • 先寫對(duì)象
  • 再讀對(duì)象
  • 接著改該P(yáng)erson類
  • 再讀對(duì)象,出現(xiàn)InvalidClassException
  • 原因:

    • 該類的序列版本號(hào)與從流中讀取的類描述符的版本號(hào)不匹配。
    • 該類包含位置數(shù)據(jù)類型
    • 該類沒(méi)有可訪問(wèn)的無(wú)參構(gòu)造方法

    解決辦法:給一個(gè)默認(rèn)的序列化ID

    6.transient

    瞬態(tài)關(guān)鍵字

    當(dāng)一個(gè)類的獨(dú)享需要被序列化時(shí),某些屬性不需要序列化,可用關(guān)鍵字transient修飾。

    靜態(tài)修飾的屬性也不會(huì)被序列化。

    • 因?yàn)樾蛄谢前褜?duì)象數(shù)據(jù)持久化存儲(chǔ),靜態(tài)屬于類加載時(shí)的數(shù)據(jù),不會(huì)被序列化。

    private static String name;

    private transient int age;

    總結(jié)

    以上是生活随笔為你收集整理的IO-5(InputStreamReader、OutputStreamWriter、序列化流、反序列化流、Serializable、transient)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。