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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

其他流的使用

發(fā)布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 其他流的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

??

輸入輸出流

數(shù)據(jù)流:

/*數(shù)據(jù)流DataInputStream和DataOutputStream作用:用于基本數(shù)據(jù)類型遍歷或字符串的讀取和寫入*/@Testpublic void test1() throws IOException { // 寫入操作DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));dos.writeUTF("張麻子");dos.flush();dos.writeInt(40);dos.flush();dos.writeBoolean(true);dos.flush();dos.close(); // 得到的data.txt文件無法直接讀取}@Test// 將文件中的變量讀入到到內(nèi)存中public void test2() throws IOException {DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));// 讀取順序要與寫入文件時的順序一致String name = dis.readUTF();int age = dis.readInt();boolean b = dis.readBoolean();System.out.println(name+age+b);dis.close();}

對象流:

?序列化:寫入文件中

反序列化:從文件在寫入內(nèi)存中

?

??

package ObjectInputOutputStream;import org.junit.Test;import java.io.*;public class ObjectStreamTest {/*將內(nèi)存中的java對象保存到磁盤中或通過網(wǎng)絡(luò)傳輸出去使用ObjectOutputStream實現(xiàn)*/@Testpublic void test() throws IOException {ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ObjectStream.txt"));oos.writeObject(new String("我愛中國!"));oos.flush();oos.close();}/*反序列化:將磁盤文件(網(wǎng)絡(luò))中的對象還原為內(nèi)存中的對象使用ObjectInputStream*/@Testpublic void test2() throws IOException, ClassNotFoundException {ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ObjectStream.txt"));Object obj = ois.readObject();System.out.println(obj);ois.close();} }

類的序列化有一定的要求:必須實現(xiàn)以下兩個接口之一

?

static,transient的變量無法序列化?

?

若沒有手動加serialVersionUID ,在修改對象后,JVM的UID會相應(yīng)的改變導(dǎo)致無法反序列化?

總結(jié)

以上是生活随笔為你收集整理的其他流的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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