一、流的概念
在Java中,流是從源到目的地的字節(jié)的有序序列。Java中有兩種基本的流——輸入流(InputStream)和輸出流(OutputStream)。
根據(jù)流相對(duì)于程序的另一個(gè)端點(diǎn)的不同,分為節(jié)點(diǎn)流和過濾流。
(1)節(jié)點(diǎn)流:以特定源如磁盤文件、內(nèi)存某區(qū)域或者線程之間的管道為端點(diǎn)的構(gòu)造輸入輸出流,是一種基本的流。
(2)過濾流:以其他已經(jīng)存在的流為端點(diǎn)構(gòu)造的輸入輸出流。
根據(jù)流中的數(shù)據(jù)單位分為字節(jié)流和字符流。
(1)字節(jié)流:流中的數(shù)據(jù)是以8位字節(jié)為單位進(jìn)行讀寫,以InputStream和OutputStream為基礎(chǔ)類。
(2)字符流:流中的數(shù)據(jù)是以16為字符為單位進(jìn)行讀寫,以Reader和Writer為基礎(chǔ)類。
二、字節(jié)流
InputStream和OutputStream是字節(jié)流的兩個(gè)頂層父類,提供了輸入流類和輸出流類的通用API。
1、輸入字節(jié)流
InputStream基本方法:
(1)基本讀方法;:int read()int read(byte[]b)int read(byte[]b,int off,int len)
(2)關(guān)閉流:void close()
(3)返回輸入流中還有多少可讀字節(jié)int available()
(4)跳過指定字節(jié)long skip(long n)
(5)回讀數(shù)據(jù)boolean markSupported()void Mark(int readlimt)void reset()
2、輸出字符流
OutputStream基本方法:
(1)基本寫方法:void write(int c)void write(byte[]b)void write(byte[]b,int off,int len)
(2)關(guān)閉流:void close()
(3)q強(qiáng)制輸出:void flush()
三、字符流
Reader和Writer是字符流的頂層父類,字符流能夠處理Unicode字符集中的所有字符。成員方法和字節(jié)流類似。
四、輸入輸出流的套接
節(jié)點(diǎn)流在程序中不是很常見,一般通過過濾流將多個(gè)流套接起來,利用各個(gè)流的特性共同處理數(shù)據(jù)。
例如下圖:一個(gè)文件流為了為了提高效率,套接了緩存流,最后套接了數(shù)據(jù)流。
五、常見的輸入輸出流
1、文件流:文件流屬于節(jié)點(diǎn)流,包括FileInputStream和FileOutputStream類以及Reader和Writer類。這些類都是對(duì)文件系統(tǒng)中的文件進(jìn)行讀寫。文件流的創(chuàng)建是調(diào)用相應(yīng)的構(gòu)造方法,并經(jīng)常以字符串形式的文件名或者一個(gè)File類的對(duì)象為參數(shù),例如:
1 public FileInputStream(String name);
2 public FileInputStream(File file);
例如使用文件流實(shí)現(xiàn)文件的復(fù)制:
package?ch01;import?java.io.File;import?java.io.FileInputStream;import?java.io.FileNotFoundException;import?java.io.FileOutputStream;import?java.io.IOException;import?java.io.InputStream;import?java.io.OutputStream;public?class?CopyBytes?{????public?static?void?main(String[]?args)?throws?IOException?{????????FileInputStream?fileis?=?new?FileInputStream(new?File("E:file/FileTest01.txt"));????????FileOutputStream?fileos?=?new?FileOutputStream(new?File("E:file/FileTest02.txt"));?????????int?c;?????????while?((c=fileis.read())?!=?-1)?{??????????????fileos.write(c);?????????????System.out.println(c);?????????}?????????fileis.close();?????????fileos.close();?????????System.out.println("OK");?????}}
2、使用文件字符流實(shí)現(xiàn)
package?ch07;import?java.io.FileReader;import?java.io.FileWriter;import?java.io.IOException;public?class?Copy?{public?static?void?main(String[]?args)?throws?IOException{????//創(chuàng)建文件字符輸入/輸出流????FileReader?in?=new?FileReader("E:workspacetmpfilefarrago.doc");????FileWriter?out=new?FileWriter("E:workspacetmpfileoutagainb.doc");????int?c;????while((c=in.read())!=-1){????????System.out.println(c);????????out.write(c);????}????in.close();????out.close();}}
六、緩沖流
緩沖流BufferedInputStream和BufferedOutputStream類和BufferedReader和BufferedWriter類。這種流把數(shù)據(jù)從原始流成塊讀入或者積累到一個(gè)大數(shù)據(jù)快的時(shí)候在成批寫出。BufferedOutputStream和BufferedWrite僅僅在緩沖區(qū)滿或者調(diào)用flush()時(shí)候才把數(shù)據(jù)寫到目的地。
構(gòu)造方法:
public BufferedInputStream(InputStream in)public BufferedInputStream(InputStream in,int size)
七、管道流
管道流可以實(shí)現(xiàn)線程之間的數(shù)據(jù)的直接傳輸。
1、管道流模型:
PipedReader/PipedInputStream實(shí)現(xiàn)管道的輸入流,而PipedWriter和PipedOutputStream實(shí)現(xiàn)輸出流。
2、管道流的創(chuàng)建:
管道流的創(chuàng)建是將管道輸入流和管道輸出流進(jìn)行掛連。構(gòu)造方法有下面兩種方式:
PipedInputStream?pInputStream=new?PipedInputStream();PipedOutputStream?pOutputStream=new?PipedOutputStream(pInputStream);或者PipedInputStream?pInputStream=new?PipedInputStream();PipedOutputStream?pOutputStream=new?PipedOutputStream();pInputStream.connect(pOutputStream);pOutputStream.connect(pInputStream);
以上就是長(zhǎng)沙達(dá)內(nèi)教育java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“Java中的輸入流與輸出流”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問,請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。