Java流類圖結(jié)構(gòu)
描述:
Java的核心庫java.io提供了全面的IO接口。包括:文件讀寫、標(biāo)準(zhǔn)設(shè)備輸出等。Java中IO是以流為基礎(chǔ)進(jìn)行輸入輸出的,所有數(shù)據(jù)被串行化寫入輸出流,或者從輸入流讀入。
Java流輸入輸出原理
Java把這些不同來源和目標(biāo)的數(shù)據(jù)都統(tǒng)一抽象為數(shù)據(jù)流。Java語言的輸入輸出功能是十分強(qiáng)大而靈活的,美中不足的是看上去輸入輸出的代碼并不是很簡潔,因?yàn)槟阃枰b許多不同的對象。
在Java類庫中,IO部分的內(nèi)容是很龐大的,因?yàn)樗婕暗念I(lǐng)域很廣泛:標(biāo)準(zhǔn)輸入輸出,文件的操作,網(wǎng)絡(luò)上的數(shù)據(jù)流,字符串流,對象流,zip文件流。
1. IO流的分類
根據(jù)處理數(shù)據(jù)類型的不同分為:字符流和字節(jié)流
根據(jù)數(shù)據(jù)流向不同分為: 輸入流和輸出流
2. 字符流和字節(jié)流
字符流的由來: 因?yàn)閿?shù)據(jù)編碼的不同,而有了對字符進(jìn)行高效操作的流對象。本質(zhì)其實(shí)就是基于字節(jié)流讀取時(shí),去查了指定的碼表。 字節(jié)流和字符流的區(qū)別:
讀寫單位不同:字節(jié)流以字節(jié)(8bit)為單位,字符流以字符為單位,根據(jù)碼表映射字符,一次可能讀多個(gè)字節(jié)。
處理對象不同:字節(jié)流能處理所有類型的數(shù)據(jù)(如圖片、avi等),而字符流只能處理字符類型的數(shù)據(jù)。
字節(jié)流:一次讀入或讀出是8位二進(jìn)制。
字符流:一次讀入或讀出是16位二進(jìn)制。
設(shè)備上的數(shù)據(jù)無論是圖片或者視頻,文字,它們都以二進(jìn)制存儲的。二進(jìn)制的最終都是以一個(gè)8位為數(shù)據(jù)單元進(jìn)行體現(xiàn),所以計(jì)算機(jī)中的最小數(shù)據(jù)單元就是字節(jié)。意味著,字節(jié)流可以處理設(shè)備上的所有數(shù)據(jù),所以字節(jié)流一樣可以處理字符數(shù)據(jù)。
結(jié)論:只要是處理純文本數(shù)據(jù),就優(yōu)先考慮使用字符流。 除此之外都使用字節(jié)流。
輸入流和輸出流
輸入流只能進(jìn)行讀操作,輸出流只能進(jìn)行寫操作,程序中需要根據(jù)待傳輸數(shù)據(jù)的不同特性而使用不同的流。
輸入字節(jié)流 InputStream
-
InputStream是所有的輸入字節(jié)流的父類,它是一個(gè)抽象類。 -
ByteArrayInputStream、StringBufferInputStream、FileInputStream是三種基本的介質(zhì)流,它們分別從Byte 數(shù)組、StringBuffer、和本地文件中讀取數(shù)據(jù)。 -
PipedInputStream是從與其它線程共用的管道中讀取數(shù)據(jù),與Piped 相關(guān)的知識后續(xù)單獨(dú)介紹。 -
ObjectInputStream和所有FilterInputStream的子類都是裝飾流(裝飾器模式的主角)。
輸出字節(jié)流 OutputStream
-
OutputStream是所有的輸出字節(jié)流的父類,它是一個(gè)抽象類。 -
ByteArrayOutputStream、FileOutputStream是兩種基本的介質(zhì)流,它們分別向Byte 數(shù)組、和本地文件中寫入數(shù)據(jù)。 -
PipedOutputStream是向與其它線程共用的管道中寫入數(shù)據(jù)。 -
ObjectOutputStream和所有FilterOutputStream的子類都是裝飾流。
總結(jié):
- 輸入流:InputStream或者Reader:從文件中讀到程序中;
- 輸出流:OutputStream或者Writer:從程序中輸出到文件中;
節(jié)點(diǎn)流
節(jié)點(diǎn)流:直接與數(shù)據(jù)源相連,讀入或讀出。
直接使用節(jié)點(diǎn)流,讀寫不方便,為了更快的讀寫文件,才有了處理流。

常用的節(jié)點(diǎn)流
- 父 類 :
InputStream、OutputStream、Reader、Writer - 文 件 :
FileInputStream、FileOutputStrean、FileReader、FileWriter文件進(jìn)行處理的節(jié)點(diǎn)流 - 數(shù) 組 :
ByteArrayInputStream、ByteArrayOutputStream、CharArrayReader、CharArrayWriter對數(shù)組進(jìn)行處理的節(jié)點(diǎn)流(對應(yīng)的不再是文件,而是內(nèi)存中的一個(gè)數(shù)組) - 字符串 :
StringReader、StringWriter對字符串進(jìn)行處理的節(jié)點(diǎn)流 - 管 道 :
PipedInputStream、PipedOutputStream、PipedReader、PipedWriter對管道進(jìn)行處理的節(jié)點(diǎn)流
處理流
處理流和節(jié)點(diǎn)流一塊使用,在節(jié)點(diǎn)流的基礎(chǔ)上,再套接一層,套接在節(jié)點(diǎn)流上的就是處理流。如BufferedReader.處理流的構(gòu)造方法總是要帶一個(gè)其他的流對象做參數(shù)。一個(gè)流對象經(jīng)過其他流的多次包裝,稱為流的鏈接。
常用的處理流
- 緩沖流:
BufferedInputStrean、BufferedOutputStream、BufferedReader、BufferedWriter增加緩沖功能,避免頻繁讀寫硬盤。 - 轉(zhuǎn)換流:
InputStreamReader、OutputStreamReader實(shí)現(xiàn)字節(jié)流和字符流之間的轉(zhuǎn)換。 - 數(shù)據(jù)流:
DataInputStream、DataOutputStream等-提供將基礎(chǔ)數(shù)據(jù)類型寫入到文件中,或者讀取出來。
轉(zhuǎn)換流
InputStreamReader 、OutputStreamWriter 要InputStream或OutputStream作為參數(shù),實(shí)現(xiàn)從字節(jié)流到字符流的轉(zhuǎn)換。
構(gòu)造函數(shù)
InputStreamReader(InputStream); //通過構(gòu)造函數(shù)初始化,使用的是本系統(tǒng)默認(rèn)的編碼表GBK。
InputStreamWriter(InputStream,String charSet); //通過該構(gòu)造函數(shù)初始化,可以指定編碼表。
OutputStreamWriter(OutputStream); //通過該構(gòu)造函數(shù)初始化,使用的是本系統(tǒng)默認(rèn)的編碼表GBK。
OutputStreamwriter(OutputStream,String charSet); //通過該構(gòu)造函數(shù)初始化,可以指定編碼表。
實(shí)戰(zhàn)演練
- FileInputStream類的使用:讀取文件內(nèi)容
package com.app;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class A1 {
public static void main(String[] args) {
A1 a1 = new A1();
//電腦d盤中的abc.txt 文檔
String filePath = "D:/abc.txt" ;
String reslut = a1.readFile( filePath ) ;
System.out.println( reslut );
}
/**
* 讀取指定文件的內(nèi)容
* @param filePath : 文件的路徑
* @return 返回的結(jié)果
*/
public String readFile( String filePath ){
FileInputStream fis=null;
String result = "" ;
try {
// 根據(jù)path路徑實(shí)例化一個(gè)輸入流的對象
fis = new FileInputStream( filePath );
//2\. 返回這個(gè)輸入流中可以被讀的剩下的bytes字節(jié)的估計(jì)值;
int size = fis.available() ;
//3\. 根據(jù)輸入流中的字節(jié)數(shù)創(chuàng)建byte數(shù)組;
byte[] array = new byte[size];
//4.把數(shù)據(jù)讀取到數(shù)組中;
fis.read( array ) ;
//5.根據(jù)獲取到的Byte數(shù)組新建一個(gè)字符串,然后輸出;
result = new String(array);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result ;
}
}
- FileOutputStream 類的使用:將內(nèi)容寫入文件
package com.app;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class A2 {
public static void main(String[] args) {
A2 a2 = new A2();
//電腦d盤中的abc.txt 文檔
String filePath = "D:/abc.txt" ;
//要寫入的內(nèi)容
String content = "今天是2017/1/9,天氣很好" ;
a2.writeFile( filePath , content ) ;
}
/**
* 根據(jù)文件路徑創(chuàng)建輸出流
* @param filePath : 文件的路徑
* @param content : 需要寫入的內(nèi)容
*/
public void writeFile( String filePath , String content ){
FileOutputStream fos = null ;
try {
//1、根據(jù)文件路徑創(chuàng)建輸出流
fos = new FileOutputStream( filePath );
//2、把string轉(zhuǎn)換為byte數(shù)組;
byte[] array = content.getBytes() ;
//3、把byte數(shù)組輸出;
fos.write( array );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
注意:
- 在實(shí)際的項(xiàng)目中,所有的IO操作都應(yīng)該放到子線程中操作,避免堵住主線程。
-
FileInputStream在讀取文件內(nèi)容的時(shí)候,我們傳入文件的路徑("D:/abc.txt"), 如果這個(gè)路徑下的文件不存在,那么在執(zhí)行readFile()方法時(shí)會(huì)報(bào)FileNotFoundException異常。 -
FileOutputStream在寫入文件的時(shí)候,我們傳入文件的路徑("D:/abc.txt"), 如果這個(gè)路徑下的文件不存在,那么在執(zhí)行writeFile()方法時(shí), 會(huì)默認(rèn)給我們創(chuàng)建一個(gè)新的文件。還有重要的一點(diǎn),不會(huì)報(bào)異常。
效果圖:
- 綜合練習(xí),實(shí)現(xiàn)復(fù)制文件,從D盤復(fù)制到E盤
package com.app;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class A3 {
public static void main(String[] args) {
A3 a2 = new A3();
//電腦d盤中的cat.png 圖片的路徑
String filePath1 = "D:/cat.png" ;
//電腦e盤中的cat.png 圖片的路徑
String filePath2 = "E:/cat.png" ;
//復(fù)制文件
a2.copyFile( filePath1 , filePath2 );
}
/**
* 文件復(fù)制
* @param filePath_old : 需要復(fù)制文件的路徑
* @param filePath_new : 復(fù)制文件存放的路徑
*/
public void copyFile( String filePath_old , String filePath_new){
FileInputStream fis=null ;
FileOutputStream fout = null ;
try {
// 根據(jù)path路徑實(shí)例化一個(gè)輸入流的對象
fis = new FileInputStream( filePath_old );
//2\. 返回這個(gè)輸入流中可以被讀的剩下的bytes字節(jié)的估計(jì)值;
int size = fis.available() ;
//3\. 根據(jù)輸入流中的字節(jié)數(shù)創(chuàng)建byte數(shù)組;
byte[] array = new byte[size];
//4.把數(shù)據(jù)讀取到數(shù)組中;
fis.read( array ) ;
//5、根據(jù)文件路徑創(chuàng)建輸出流
fout = new FileOutputStream( filePath_new ) ;
//5、把byte數(shù)組輸出;
fout.write( array );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if ( fout != null ) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}