Java是一種可以撰寫跨平臺應(yīng)用軟件的面向?qū)ο蟮某绦蛟O(shè)計語言。Java 技術(shù)具有卓越的通用性、高效性、平臺移植性和安全性,廣泛應(yīng)用于PC、數(shù)據(jù)中心、游戲控制臺、科學(xué)超級計算機、移動電話和互聯(lián)網(wǎng),同時擁有全球最大的開發(fā)者專業(yè)社群。
給你學(xué)習(xí)路線:html-css-js-jq-javase-數(shù)據(jù)庫-jsp-servlet-Struts2-hibernate-mybatis-spring4-springmvc-ssh-ssm

一、概念
在Java中,文件的輸入和輸出是通過流(Stream)來實現(xiàn)的。一個流,必有源端和目的端,它們可以是計算機內(nèi)存的某些區(qū)域,也可以是磁盤文件,甚至可以是 Internet 上的某個 URL。對于流而言,我們不用關(guān)心數(shù)據(jù)是如何傳輸?shù)?,只需要向源端輸入?shù)據(jù),從目的端獲取數(shù)據(jù)即可。
流按照處理數(shù)據(jù)的單位,可以分為字節(jié)流和字符流。字節(jié)流的處理單位是字節(jié),通常用來處理二進制文件,例如音樂、圖片文件等。而字符流的處理單位是字符,因為Java采用Unicode編碼,Java字符流處理的即為Unicode字符,所以在操作漢字、國際化等方面,字符流具有優(yōu)勢。

小編推薦一個學(xué)Java的學(xué)習(xí)裙【 七六零,二五零,五四一 】,無論你是大牛還是小白,是想轉(zhuǎn)行還是想入行都可以來了解一起進步一起學(xué)習(xí)!裙內(nèi)有開發(fā)工具,很多干貨和技術(shù)資料分享!
二、字節(jié)流
所有的字節(jié)流類都繼承自InputStream 和 OutputStream 這兩個抽象類,下面列舉了5個輸入字節(jié)流類,輸出字節(jié)流類和輸入字節(jié)流類存在對應(yīng)關(guān)系,這個就不一一列舉了。
FileInputStream:把一個文件作為輸入源,從本地文件系統(tǒng)中讀取數(shù)據(jù)字節(jié),實現(xiàn)對文件的讀取操作。
ByteArrayInputStream:把內(nèi)存中的一個緩沖區(qū)作為輸入源,從內(nèi)存數(shù)組中讀取數(shù)據(jù)字節(jié)。
ObjectInputStream:對以前使用過ObjectOuputStream寫入的基本數(shù)據(jù)和對象進行反序列化,用于恢復(fù)那些以前序列化的對象,注意這個對象所屬的類必須實現(xiàn)Serializable接口。
PipeInputStream:實現(xiàn)了管道的概念,從線程通道中讀取線程字節(jié)。主要在線程中使用,用于兩個線程間通信。
SequenceInputStream:表示其他輸入流的邏輯串聯(lián)。它從輸入流的有序集合開始,并從第一個輸入流開始讀取,直到到達文件末尾,接著從第二個輸入流讀取,依次類推,直到到達包含的最后一個輸入流的文件末尾為止。
System.in:從用戶控制臺讀取數(shù)據(jù)字節(jié),在System類中,in是 InputStream 類的靜態(tài)導(dǎo)入。
public static void main(String[] args) { InputStream in = null; OutputStream out = null; try { //得到輸入流 in = new FileInputStream("E:\test\a.txt"); //得到輸出流 File file = new File("E:\test\b.txt"); if (!file.exists()) { file.createNewFile(); } out = new FileOutputStream(file, true); int i;//從輸入流讀取一定數(shù)量的字節(jié),返回 0 到 255 范圍內(nèi)的 int 型字節(jié)值 while ((i = in.read()) != -1) { out.write(i); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
三、字符流
所有的字符流類都繼承自Reader 和 Writer 這兩個抽象類,其中Reader是用于讀取字符流的抽象類,Writer是用于寫入字符流的抽象類。
Reader 和 Writer 要解決的最主要問題是國際化。原先的 I/O 類庫只支持8位的字節(jié)流,因此不能很好的處理16位的Unicode字符。Unicode 是國際化的字符集,這樣增加了Reader 和 Writer之后,就可以自動在本地字符集和Unicode國際化字符集之間進行轉(zhuǎn)換。
FileReader:與FileInputStream對應(yīng),從文件系統(tǒng)中讀取字符序列。
CharArrayReader:與ByteArrayInputStream 對應(yīng),從字符數(shù)組中讀取數(shù)據(jù)。
PipedReader:與PipedInputStream 對應(yīng),從線程管道中讀取字符序列。
StringReader:從字符串中讀取字符序列。
/** * 由于是字符,存在編碼不一致導(dǎo)致亂碼的問題 * @param args */ public static void main(String[] args) { Reader reader = null; Writer writer = null; try { //得到輸入流 reader = new FileReader("E:\test\a.txt"); //得到輸出流 writer = new FileWriter("E:\test\c.txt", true); char[] chars = new char[50]; int i; while ((i = reader.read(chars)) != -1) { writer.write(chars, 0, i); writer.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } if (writer != null) { writer.close(); } } catch (IOException e) { e.printStackTrace(); } } }
四、緩沖流
前面介紹的字節(jié)流、字符流都是無緩沖的輸入、輸出流,這就意味著,每一次的讀、寫操作都會交給操作系統(tǒng)來處理。這樣的做法可能會對系統(tǒng)的性能造成很大的影響,因為每一次操作都可能引起磁盤硬件的讀、寫或網(wǎng)絡(luò)的訪問。因此,對于字節(jié)流和字符流,一般不直接使用。
緩存流是一種裝飾器類,目的是讓原字節(jié)流、字符流 新增緩沖的功能。以字符緩沖流為例進行說明,字符緩沖流從字符流中讀取、寫入字符,不立刻要求系統(tǒng)進行處理,而是緩沖部分字符,從而實現(xiàn)按規(guī)定字符數(shù)、按行等方式高效ed讀取或?qū)懭搿?/p>
字節(jié)緩沖流:
public static void main(String[] args) { BufferedInputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new FileInputStream("E:\test\a.txt")); out = new BufferedOutputStream(new FileOutputStream("E:\test\e.txt", true)); byte[] b = new byte[1024]; int i; while ((i = in.read(b)) != -1) { out.write(b, 0, i); out.flush();//手動刷新該流的緩沖,立即將他們寫入預(yù)期目標 } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }

小編推薦一個學(xué)Java的學(xué)習(xí)裙【 七六零,二五零,五四一 】,無論你是大牛還是小白,是想轉(zhuǎn)行還是想入行都可以來了解一起進步一起學(xué)習(xí)!裙內(nèi)有開發(fā)工具,很多干貨和技術(shù)資料分享!
字符緩沖流:
public static void main(String[] args) { BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { //設(shè)置文件編碼,解決文件亂碼問題 //將字節(jié)流轉(zhuǎn)換為字符流,實際上使用了一種設(shè)計模式——適配器模式 InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\test\a.txt"), "GBK"); bufferedReader = new BufferedReader(isr); bufferedWriter = new BufferedWriter(new FileWriter("E:\test\d.txt")); String s; while ((s = bufferedReader.readLine()) != null) { bufferedWriter.write(s); bufferedWriter.newLine();//按行讀取,寫入一個分行符,否則所有內(nèi)容都在一行顯示 } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } if (bufferedWriter != null) { bufferedWriter.close(); } } catch (IOException e) { e.printStackTrace(); } } }


