java中的Io(input與output)操作總結(jié)(四)
這一節(jié)我們來(lái)說(shuō)說(shuō)關(guān)于java io的其他內(nèi)容
Serializable序列化
實(shí)例1:對(duì)象的序列化
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ì)象的反序列化
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:線程的通信
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ě)入
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)型的讀取
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ò)誤
- 詳解Java中ByteArray字節(jié)數(shù)組的輸入輸出流的用法
- java實(shí)現(xiàn)輸入輸出流代碼分享
- 詳解Java的文件與目錄管理以及輸入輸出相關(guān)操作
- java實(shí)現(xiàn)文本框和文本區(qū)的輸入輸出
- Java輸入輸出流的使用詳細(xì)介紹
- Java中FilterInputStream和FilterOutputStream的用法詳解
- java中的Io(input與output)操作總結(jié)(三)
- java中的Io(input與output)操作總結(jié)(二)
- java中的Io(input與output)操作總結(jié)(一)
- Java中的BufferedInputStream與BufferedOutputStream使用示例
相關(guān)文章
Springboot如何統(tǒng)一處理Filter異常
這篇文章主要介紹了Springboot如何統(tǒng)一處理Filter異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12springboot中實(shí)現(xiàn)上傳文件的功能簡(jiǎn)單示例
這篇文章主要給大家介紹了關(guān)于springboot中實(shí)現(xiàn)上傳文件功能的相關(guān)資料,在Spring Boot中實(shí)現(xiàn)文件上傳下載功能相對(duì)簡(jiǎn)單,文中給出了代碼示例,需要的朋友可以參考下2023-09-09java中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使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題
這篇文章主要介紹了使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12