欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中的Io(input與output)操作總結(jié)(四)

 更新時(shí)間:2013年01月10日 10:50:00   作者:  
前面已經(jīng)把java io的主要操作講完了,這一節(jié)我們來(lái)說(shuō)說(shuō)關(guān)于java io的其他內(nèi)容:Serializable序列化/DataOutputStream和DataInputStream類(lèi)/管道流等等,感興趣的朋友可以了解下
前面已經(jīng)把java io的主要操作講完了
這一節(jié)我們來(lái)說(shuō)說(shuō)關(guān)于java io的其他內(nèi)容

Serializable序列化
實(shí)例1:對(duì)象的序列化
復(fù)制代碼 代碼如下:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
@SuppressWarnings("serial")
//一個(gè)類(lèi)要想實(shí)現(xiàn)序列化則必須實(shí)現(xiàn)Serializable接口
class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "Name:" + this.name + ", Age:" + this.age;
}
}
public class Demo {
public static void main(String[] args) {
String path = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";
Person p1 = new Person("zhangsan",12);
Person p2 = new Person("lisi",14);
//此處創(chuàng)建文件寫(xiě)入流的引用是要給ObjectOutputStream的構(gòu)造函數(shù)玩兒
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(path);
oos = new ObjectOutputStream(fos);
//這里可以寫(xiě)入對(duì)象,也可以寫(xiě)入其他類(lèi)型數(shù)據(jù)
oos.writeObject(p1);
oos.writeObject(p2);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

所謂對(duì)象序列化就是把一個(gè)對(duì)象進(jìn)行持久化存儲(chǔ),方便保留其屬性
通俗點(diǎn)說(shuō),等于把一個(gè)對(duì)象從堆內(nèi)存里邊揪出來(lái)放到硬盤(pán)上
當(dāng)然,如果你開(kāi)心,你可以序列化其他東西,包括數(shù)組,基本數(shù)據(jù)類(lèi)型等等
來(lái)看看內(nèi)容,神馬玩意兒這是……

 
實(shí)例2:對(duì)象的反序列化

復(fù)制代碼 代碼如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class Demo {
public static void main(String[] args) {
String path = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";
//好吧,這里代碼寫(xiě)得著實(shí)有點(diǎn)長(zhǎng)了,還要拋異常什么的
//如果你也看的煩,那就在主方法上拋吧,構(gòu)造方法里用匿名對(duì)象就好了
//什么?別告訴我你不知道匿名對(duì)象
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(path);
ois = new ObjectInputStream(fis);
//這里返回的其實(shí)是一個(gè)Object類(lèi)對(duì)象
//因?yàn)槲覀円阎莻€(gè)Person類(lèi)對(duì)象
//所以,就地把它給向下轉(zhuǎn)型了
Person p = (Person)ois.readObject();
System.out.println(p);
//拋死你,煩煩煩~?。?!
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
//還是要記得關(guān)閉下流
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

你看,我們把一個(gè)對(duì)象存放在硬盤(pán)上是為了方便日后使用
現(xiàn)在用得著它了,自然得拿出來(lái)


管道流
實(shí)例3:線程的通信

復(fù)制代碼 代碼如下:

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
//實(shí)現(xiàn)Runnable接口,實(shí)現(xiàn)一個(gè)讀的線程
class Read implements Runnable {
private PipedInputStream in;
//將需要讀的管道流傳入到構(gòu)造函數(shù)中
public Read(PipedInputStream in) {
this.in = in;
}
//實(shí)現(xiàn)讀這一線程
public void run() {
try {
byte[] buf = new byte[1024];
int temp = 0;
//循環(huán)讀取
//read是一個(gè)阻塞方法,需要拋異常
//此處把打印流的代碼也加入進(jìn)來(lái)
//是因?yàn)槿绻麤](méi)有讀取到數(shù)據(jù),那么打印的代碼也無(wú)效
while((temp = in.read(buf)) != -1) {
String str = new String(buf,0,temp);
System.out.println(str);
}
} catch (IOException e) {
//其實(shí)這里應(yīng)拋出一個(gè)自定義異常的
//暫時(shí)我還沒(méi)弄清楚
e.printStackTrace();
} finally {
try {
//我已經(jīng)拋火了,這只是為了提醒自己異常很重要
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//這里實(shí)現(xiàn)一個(gè)寫(xiě)的類(lèi)
class Write implements Runnable {
private PipedOutputStream out;
//將管道輸入流傳進(jìn)來(lái)
public Write(PipedOutputStream out) {
this.out = out;
}
public void run() {
try {
//這里開(kāi)始寫(xiě)出數(shù)據(jù)
out.write("管道輸出".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//其實(shí)應(yīng)該可以把這個(gè)關(guān)閉方法寫(xiě)到上面那個(gè)try里邊
//但是這樣感覺(jué)怪怪的,邏輯不大對(duì)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Demo {
public static void main(String[] args) {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
try {
//連接管道
in.connect(out);
//創(chuàng)建對(duì)象,開(kāi)啟線程
//此處同樣放進(jìn)try...catch里面
//因?yàn)槿绻麤](méi)有鏈接管道,下面操作無(wú)意義
Read r = new Read(in);
Write w = new Write(out);
//把已經(jīng)實(shí)現(xiàn)好run方法的對(duì)象放入線程中執(zhí)行
new Thread(r).start();
new Thread(w).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

好吧,廢了那么大勁兒,就打印了這么一句話(huà),異常拋棄來(lái)很煩,為了注重細(xì)節(jié)……


管道流也許很難理解,其實(shí)非也
我們知道,字節(jié)流和字符流都需要數(shù)組來(lái)進(jìn)行流的中轉(zhuǎn)
而管道流則直接串聯(lián)兩條流,一邊發(fā)送數(shù)據(jù),一邊接收
然而,同時(shí)通信的的兩種狀態(tài),如何才能確定發(fā)送和接收的一致性呢
那么,就需要用到線程,無(wú)論是接收方還是發(fā)送方先執(zhí)行
總會(huì)造成一個(gè)線程的阻塞狀態(tài),從而等待另一方的數(shù)據(jù)傳過(guò)來(lái)
總體而言,管道流的目的,也就是為了線程通信
此外,還有PipedReader和PipedWriter類(lèi),操作原理都一樣,這里就不再贅述了
DataOutputStream和DataInputStream類(lèi)
實(shí)例4:基本數(shù)據(jù)類(lèi)型的寫(xiě)入

復(fù)制代碼 代碼如下:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) {
String path = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";
DataOutputStream d = null;
try {
//此處需要傳入一個(gè)OutputStream類(lèi)的對(duì)象
d = new DataOutputStream(new FileOutputStream(path));
//開(kāi)始寫(xiě)入基本數(shù)據(jù)類(lèi)型
d.writeInt(12);
d.writeBoolean(true);
d.writeDouble(12.2223);
d.writeChar(97);
//刷新流
d.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
d.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

此處我們并不能直觀看懂內(nèi)容,因?yàn)樗捎米止?jié)流的方式操作,而不是字符流
我們只需要知道,此程序已經(jīng)將基本數(shù)據(jù)類(lèi)型寫(xiě)入到硬盤(pán)即可


實(shí)例5:基本數(shù)據(jù)類(lèi)型的讀取

復(fù)制代碼 代碼如下:

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) {
String path = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";
DataInputStream d = null;
try {
d = new DataInputStream(new FileInputStream(path));
//按存儲(chǔ)順序讀取基本數(shù)據(jù)類(lèi)型
System.out.println(d.readInt());
System.out.println(d.readBoolean());
System.out.println(d.readDouble());
System.out.println(d.readChar());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
d.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

這里要注意的是,一定要按照寫(xiě)入順序讀取,否則會(huì)發(fā)生數(shù)據(jù)的打印錯(cuò)誤

相關(guān)文章

  • Springboot如何統(tǒng)一處理Filter異常

    Springboot如何統(tǒng)一處理Filter異常

    這篇文章主要介紹了Springboot如何統(tǒng)一處理Filter異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java 鏈表的定義與簡(jiǎn)單實(shí)例

    Java 鏈表的定義與簡(jiǎn)單實(shí)例

    這篇文章主要介紹了 Java 鏈表的定義與簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • java15新功能的詳細(xì)講解

    java15新功能的詳細(xì)講解

    這篇文章主要介紹了java15的新功能,雖然java15并不是長(zhǎng)期支持的版本,但是很多新功能還是很有用的。感興趣的小伙伴可以參考一下
    2021-08-08
  • Java自動(dòng)生成編號(hào)的方法步驟

    Java自動(dòng)生成編號(hào)的方法步驟

    在新增數(shù)據(jù)時(shí),往往需要自動(dòng)生成編號(hào),本文主要介紹了Java自動(dòng)生成編號(hào)的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Java Swagger技術(shù)使用指南

    Java Swagger技術(shù)使用指南

    Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)。總體目標(biāo)是使客戶(hù)端和文件系統(tǒng)作為服務(wù)器以同樣的速度來(lái)更新。文件的方法,參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來(lái)始終保持同步
    2021-09-09
  • springboot中實(shí)現(xiàn)上傳文件的功能簡(jiǎn)單示例

    springboot中實(shí)現(xiàn)上傳文件的功能簡(jiǎn)單示例

    這篇文章主要給大家介紹了關(guān)于springboot中實(shí)現(xiàn)上傳文件功能的相關(guān)資料,在Spring Boot中實(shí)現(xiàn)文件上傳下載功能相對(duì)簡(jiǎn)單,文中給出了代碼示例,需要的朋友可以參考下
    2023-09-09
  • java中Statement 與 PreparedStatement接口之間的關(guān)系和區(qū)別

    java中Statement 與 PreparedStatement接口之間的關(guān)系和區(qū)別

    這篇文章主要介紹了java中Statement 與 PreparedStatement接口之間的關(guān)系和區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • RabbitMQ使用案例詳解

    RabbitMQ使用案例詳解

    RabbitMQ是基于Erlang語(yǔ)言開(kāi)發(fā)的開(kāi)源的消息中間件,這篇文章給大家介紹RabbitMQ使用案例,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • JAVA進(jìn)程突然消失問(wèn)題解決方案

    JAVA進(jìn)程突然消失問(wèn)題解決方案

    這篇文章主要介紹了JAVA進(jìn)程突然消失問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題

    使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題

    這篇文章主要介紹了使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評(píng)論