【正文】
hba c k I npu t S t r e a m L i n e N um be r I npu t S t r e a m D a t a I npu t S t r e a m B uf f e r e dI npu t S t r e a m O ut put S t r e a m P i pe dO ut put S t r e a m F i l t e r O ut put St r eam F i l e O ut put S t e a m B yt e A r r a y O ut put S t r e a m P r i nt S t r e a m D a t a O ut put S t r e a m B uf f e r e d O ut put S t r e a m ? InputStream類是所有輸入流類的父類,其子類能夠完成不同的輸入功能。 ? InputStream類和 OutputStream類都是抽象類。 InputStream類的常用方法 方法 功能描述 public InputStream() 構(gòu)造方法,子類調(diào)用 public int read() 從輸入流中的當(dāng)前位置讀入一個字節(jié)的二進(jìn)制數(shù)據(jù),返回值是 0~255之間的一個整數(shù),該方法是 abstract的,需子類實現(xiàn) public int read(byte b[]) 從輸入流中讀取長度為 b,返回值為讀取的字節(jié)數(shù) public int read(byte b[],int off,int len) 從輸入流中的當(dāng)前位置讀取 len個字節(jié)并保存入數(shù)組 b中從下標(biāo) off+1開始的位置,返回值為讀取的字節(jié)數(shù) public int available() 返回值為輸入流可以讀取的字節(jié)數(shù) public void mark() 在輸入流的當(dāng)前位置作為標(biāo)記 public void reset() 將位置指針返回到標(biāo)記位置 public long skip(long n) 將輸入流中當(dāng)前位置向后移動 n個字節(jié),返回值為跳過的字節(jié)數(shù) public void close() 關(guān)閉輸入流 InputStream類使用說明 ? 注意事項: ? ( 1)流在使用完成后,應(yīng)顯式調(diào)用 close()方法關(guān)閉流。 ? 當(dāng) Java程序需從外部設(shè)備如鍵盤、磁盤文件等讀取數(shù)據(jù)時,需創(chuàng)建一個相應(yīng)類型的輸入流對象來完成與該外部設(shè)備的連接。 OutputStream類 ? OutputStream類是一個抽象類,包含一套所有輸出都需要的方法,可以完成最基本的從輸出流寫入數(shù)據(jù)的操作。這樣處理的好處是,可以降低 CPU對外部設(shè)備的讀寫次數(shù),提高系統(tǒng)效率。 文件處理 ? 文件處理是最常見的 I/O處理。 ? 本小節(jié)將簡單介紹一些常用的文件類:File類、 FileInputStream類和FileOutputStream類、 Writer類和 Reader類。 ? 在 Java語言中 , 通過文件 ( File) 類提供的方法可以訪問文件的這些信息 。 ? 要進(jìn)行文件處理,首要的是了解 File類提供的各種方法。每個 File類對象表示一個磁盤文件或目錄,其對象屬性中包含了文件或目錄的相關(guān)信息,如文件名、文件長度等,調(diào)用它的方法可以完成對文件或目錄的管理操作,如創(chuàng)建、刪除、改名等。 import 。 File f=new File(dir,)。 File f2=new File(d:\\book\\)。 (+f+的有關(guān)信息 )。 (name: +())。 (absolute path:+())。 【 例 】 File類的應(yīng)用 (is a directory:+())。 (can read: +())。 File newf=new File()。 (newf)。 (f+ exist: +())。 (pare f to f2: +(f2))。 (newf+ exist: +())。 通過這兩個類可以打開本地文件 , 以字節(jié)為單位從文件中順序讀取數(shù)據(jù)或向文件中寫入數(shù)據(jù) 。 【 例 】 利用 FileInputStream類讀取文件內(nèi)容 // 利用 FileInputStream讀取文件 import .*。 //創(chuàng)建 FileInputStream對象 (文件長度為: +())。 // 建立 byte數(shù)組 (b)。 (new String(b))。 ()。若不在同一目錄下則使用絕對路徑。 ? ( 2)在獲取文件長度時應(yīng)注意,一個漢字占兩個字節(jié),一個回車分解為“ \r\n”兩個轉(zhuǎn)義字符,占兩個字節(jié)。 2. FileOutputStream類 FileOutputStream類常用的構(gòu)造方法 構(gòu)造方法 功能描述 public FileOutputStream(String name) 以 name為文件名創(chuàng)建FileOutputStream對象,原先的文件會被覆蓋 public FileOutputStream(String name,boolean a) 同上,但若 a為 true,則數(shù)據(jù)追加在原數(shù)據(jù)之后 public FileOutputStream(File file) 以 File對象為文件創(chuàng)建FileOutputStream對象 FileOutputStream類的常用方法 方法 功能描述 public void close() 關(guān)閉文件輸出流 public int write() 從文件輸出流中讀取一個字節(jié)的數(shù)據(jù) public int write(byte[] b) 將字節(jié)數(shù)組 b中的數(shù)據(jù)寫入文件輸出流 public int write(byte[] b,int off,int len) 將字節(jié)數(shù)組 b中從下標(biāo) off開始的 len個字節(jié)寫入文件輸出流 注意:在調(diào)用類 FileOutputStream的相關(guān)方法來完成文件操作時均有可能拋出 IOException異常。 public class Copy_Pic { public static void main(String args[]) throws IOException { FileInputStream fin=new FileInputStream()。//復(fù)制到的圖片文件 FileOutputStream fout=new FileOutputStream(file)。 // 輸出文件大小 byte b[]=new byte[()]。 // 將圖片讀入 b數(shù)組 (b)。 (())。 ()。若不在同一目錄下則使用絕對路徑。若文件不存在或創(chuàng)建不成功則拋出IOException異常。它們都是抽象類,提供了一些用于字符流處理的接口,不能用來創(chuàng)建對象,主要用來派生字符流子類。 1. Reader類 Reader類是定義 Java字符流輸入模式的抽象類。Writer類的主要方法 : 方法 功能描述 public abstract void close() 關(guān)閉流 public void write(int c) 向流中寫入一個字符,寫入字符存放于 c的低16位中 public void write(char[] cbuf) 將字符數(shù)組 cbuf的內(nèi)容寫入流 public abstract void write(char[] cbuf,int off,int len) 將字符數(shù)組 cbuf中從下標(biāo) off開始的 len個字符寫入流 public void write(String str) 將字符串 str寫入流 public void write(String str,int off,int len) 將字符串中從 off開始的 len個連續(xù)字符寫入流 public abstract void flush() 刷新流,強制輸出緩沖區(qū)中的所有字符 public void reset() 將流標(biāo)記重置到上一次做標(biāo)記處 ? 一般而言, Reader類和 Writer類的方法通常不能直接使用,而是通過其派生類來使用。 3. FileReader類和 FileWriter類 ? ( 1) FileReader類 ? FileReader類是一個以字符方式讀取文件內(nèi)容的 Reader類的派生類。 構(gòu)造方法 功能描述 public FileReader(String name) 以 name為文件名創(chuàng)建 FileReader對象 public FileReader(File file) 以 File對象為文件創(chuàng)建 FileReader對象 ? FileWriter類是一個以字符方式將數(shù)據(jù)寫入文件的 Writer類的派生類, FileWriter對象的創(chuàng)建不依賴于文件是否存在。若試圖打開一個只讀文件,則拋出 IOException異常。 public class Copy_File { public static void main(String args[]) throws IOException { char data[]=new char[1024]。 // 建立對象 fr FileWriter fw=new FileWriter(d:\\book\\)。 // 將數(shù)據(jù)讀入字符列表 data內(nèi) (data)。 String str=new String(data,0,num)。 ()。 }} 4. BufferedReader類和BufferedWriter類 ? ( 1) BufferedReader類 ? BufferedReader類繼承自 Reader類,用來讀取緩沖區(qū)里的數(shù)據(jù)。 ? 使用 BufferedReader類來讀取緩沖區(qū)中的數(shù)據(jù)之前,必須先創(chuàng)建 FileReader對象,再以該對象為參數(shù)創(chuàng)建 BufferedReader類對象,然后才可利用該對象讀取緩沖區(qū)中的數(shù)據(jù)。 ? 使用 BufferedWriter類將數(shù)據(jù)寫入緩沖區(qū)時,首先必須先創(chuàng)建 FileWriter對象,再以該對象為參數(shù)創(chuàng)建 BufferedWriter類對象,然后即可利用該對象將數(shù)據(jù)寫入緩沖區(qū)。 BufferedWriter類常用的構(gòu)造方法 構(gòu)造方法 功能描述 public BufferedWriter(Writer out) 創(chuàng)建緩沖區(qū)字符輸出流 public BufferedWriter(Writer out,int sz) 創(chuàng)建緩沖區(qū)字符輸出流,并設(shè)置緩沖區(qū)大小 BufferedWriter類的主要方法 方法 功能描述 public void close() 關(guān)閉流 public void write(int c) 向流中寫入一個字符,寫入字符存放于 c的低 16位中 public void write(char[] cbuf,int off,int len) 將字符數(shù)組 cbuf中從下標(biāo) off開始的 len個字符寫入流 public void write(String str,int off,int len) 將字符串中從 off開始的 len個連續(xù)字符寫入流 public void newLine() 寫入換行符 public void flush() 刷新流,強制輸出緩沖區(qū)中的所有字符 【 例 】 利用 BufferedReader類和 BufferedWriter類完成文件復(fù)制 // 使用 BufferedReader類和 BufferedWriter類完成文件復(fù)制 import .*。 FileReader fr=new FileReader(d:\\book\\)。 BufferedReader br=new BufferedReader(fr)。 while((data=())!=null)//從文件 { (data)。//將讀取的數(shù)據(jù)寫入文件 ()。//將緩沖區(qū)中數(shù)據(jù)全部輸出到文件 (文件復(fù)制完成!請查看相關(guān)文件內(nèi)容! )。 ()。另外它還提供了對象的同步機制,可保證在某一時刻只有一個線程可以訪問一個 I/O流。 ? 在 , FilterInputStream和FilterOutputStream類是所有過濾流類的父類,是抽象類。 過濾流類繼承關(guān)系 F i l t e r I npu t S t r e a m P us hba c k I npu t S t r e a m L i n e N um be r I npu t S t r e a m D a t a I npu t S t r e a m B uf f e