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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java中input与output_java中的Io(input与output)操作总结(四)

發(fā)布時間:2024/4/17 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java中input与output_java中的Io(input与output)操作总结(四) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前面已經(jīng)把java io的主要操作講完了

這一節(jié)我們來說說關(guān)于java io的其他內(nèi)容

Serializable序列化

實例1:對象的序列化

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.Serializable;

@SuppressWarnings("serial")

//一個類要想實現(xiàn)序列化則必須實現(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)建文件寫入流的引用是要給ObjectOutputStream的構(gòu)造函數(shù)玩兒

FileOutputStream fos = null;

ObjectOutputStream oos = null;

try {

fos = new FileOutputStream(path);

oos = new ObjectOutputStream(fos);

//這里可以寫入對象,也可以寫入其他類型數(shù)據(jù)

oos.writeObject(p1);

oos.writeObject(p2);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

oos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

所謂對象序列化就是把一個對象進行持久化存儲,方便保留其屬性

通俗點說,等于把一個對象從堆內(nèi)存里邊揪出來放到硬盤上

當然,如果你開心,你可以序列化其他東西,包括數(shù)組,基本數(shù)據(jù)類型等等

來看看內(nèi)容,神馬玩意兒這是……

?

實例2:對象的反序列化

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";

//好吧,這里代碼寫得著實有點長了,還要拋異常什么的

//如果你也看的煩,那就在主方法上拋吧,構(gòu)造方法里用匿名對象就好了

//什么?別告訴我你不知道匿名對象

FileInputStream fis = null;

ObjectInputStream ois = null;

try {

fis = new FileInputStream(path);

ois = new ObjectInputStream(fis);

//這里返回的其實是一個Object類對象

//因為我們已知它是個Person類對象

//所以,就地把它給向下轉(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();

}

}

}

}

你看,我們把一個對象存放在硬盤上是為了方便日后使用

現(xiàn)在用得著它了,自然得拿出來

管道流

實例3:線程的通信

import java.io.IOException;

import java.io.PipedInputStream;

import java.io.PipedOutputStream;

//實現(xiàn)Runnable接口,實現(xiàn)一個讀的線程

class Read implements Runnable {

private PipedInputStream in;

//將需要讀的管道流傳入到構(gòu)造函數(shù)中

public Read(PipedInputStream in) {

this.in = in;

}

//實現(xiàn)讀這一線程

public void run() {

try {

byte[] buf = new byte[1024];

int temp = 0;

//循環(huán)讀取

//read是一個阻塞方法,需要拋異常

//此處把打印流的代碼也加入進來

//是因為如果沒有讀取到數(shù)據(jù),那么打印的代碼也無效

while((temp = in.read(buf)) != -1) {

String str = new String(buf,0,temp);

System.out.println(str);

}

} catch (IOException e) {

//其實這里應(yīng)拋出一個自定義異常的

//暫時我還沒弄清楚

e.printStackTrace();

} finally {

try {

//我已經(jīng)拋火了,這只是為了提醒自己異常很重要

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

//這里實現(xiàn)一個寫的類

class Write implements Runnable {

private PipedOutputStream out;

//將管道輸入流傳進來

public Write(PipedOutputStream out) {

this.out = out;

}

public void run() {

try {

//這里開始寫出數(shù)據(jù)

out.write("管道輸出".getBytes());

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

//其實應(yīng)該可以把這個關(guān)閉方法寫到上面那個try里邊

//但是這樣感覺怪怪的,邏輯不大對

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)建對象,開啟線程

//此處同樣放進try...catch里面

//因為如果沒有鏈接管道,下面操作無意義

Read r = new Read(in);

Write w = new Write(out);

//把已經(jīng)實現(xiàn)好run方法的對象放入線程中執(zhí)行

new Thread(r).start();

new Thread(w).start();

} catch (IOException e) {

e.printStackTrace();

}

}

}

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

管道流也許很難理解,其實非也

我們知道,字節(jié)流和字符流都需要數(shù)組來進行流的中轉(zhuǎn)

而管道流則直接串聯(lián)兩條流,一邊發(fā)送數(shù)據(jù),一邊接收

然而,同時通信的的兩種狀態(tài),如何才能確定發(fā)送和接收的一致性呢

那么,就需要用到線程,無論是接收方還是發(fā)送方先執(zhí)行

總會造成一個線程的阻塞狀態(tài),從而等待另一方的數(shù)據(jù)傳過來

總體而言,管道流的目的,也就是為了線程通信

此外,還有PipedReader和PipedWriter類,操作原理都一樣,這里就不再贅述了

DataOutputStream和DataInputStream類

實例4:基本數(shù)據(jù)類型的寫入

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 {

//此處需要傳入一個OutputStream類的對象

d = new DataOutputStream(new FileOutputStream(path));

//開始寫入基本數(shù)據(jù)類型

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)容,因為它采用字節(jié)流的方式操作,而不是字符流

我們只需要知道,此程序已經(jīng)將基本數(shù)據(jù)類型寫入到硬盤即可

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

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));

//按存儲順序讀取基本數(shù)據(jù)類型

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();

}

}

}

}

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

總結(jié)

以上是生活随笔為你收集整理的Java中input与output_java中的Io(input与output)操作总结(四)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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