Java Web應(yīng)用程序開發(fā) (2)教學(xué)課件_第1頁
Java Web應(yīng)用程序開發(fā) (2)教學(xué)課件_第2頁
Java Web應(yīng)用程序開發(fā) (2)教學(xué)課件_第3頁
Java Web應(yīng)用程序開發(fā) (2)教學(xué)課件_第4頁
Java Web應(yīng)用程序開發(fā) (2)教學(xué)課件_第5頁
已閱讀5頁,還剩41頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、 I/O流企業(yè)級卓越人才培養(yǎng)天津?yàn)I海迅騰科技集團(tuán)有限公司第二章目標(biāo)目標(biāo)q了解I/O流概念 q理解I/O層次結(jié)構(gòu)q掌握使用字節(jié)流q掌握使用字符流概述概述q絕大部分的編程工作都涉及到數(shù)據(jù)的傳遞控制 (輸入/輸出)qJAVA中的I/O采用流的方式來實(shí)現(xiàn),即將數(shù)據(jù)的傳遞看作為數(shù)據(jù)在目的和源之間流動 qI/O類庫位于java.io包中,對各種常見的輸入和輸出流進(jìn)行了抽象概述概述 按照I/O處理數(shù)據(jù)的不同分為字節(jié)流字符流 按照功能的不同分為數(shù)據(jù)流操作流 按實(shí)現(xiàn)方式的不同分為底層流高層流字節(jié)流字節(jié)流q將字節(jié)作為最小傳輸單元q由相應(yīng)的數(shù)據(jù)流和操作流構(gòu)成q字節(jié)流類層次圖如下(包括輸入流和輸出流) 字節(jié)流字節(jié)流

2、數(shù)據(jù)流(底層流):InputStreamOutputStreamFileInputStreamFileOutStream 操作流(高層流):BufferedInputStreamBufferedOutputStreamPrintStream數(shù)據(jù)流數(shù)據(jù)流q負(fù)責(zé)搭建字節(jié)數(shù)據(jù)傳輸?shù)耐ǖ?,?fù)責(zé)字節(jié)數(shù)據(jù)的傳輸,是底層流,只提供了基本的字節(jié)數(shù)據(jù)訪問方法qInputStream和OutputStram是所有字節(jié)流類的基類,都是抽象類,不能實(shí)例化,通過定義了若干的方法來規(guī)范派生類的行為,如I/O結(jié)束后必須關(guān)閉流等 數(shù)據(jù)流數(shù)據(jù)流InputStreamq所有字節(jié)輸入流類的抽象基類,完成將字節(jié)從流中讀出 publi

3、c abstract int read() throws IOException public int read(byte b)throws IOException public void close() throws IOException public int available()throws IOExceptionq重要方法:數(shù)據(jù)流數(shù)據(jù)流OutputStreamq所有字節(jié)輸出流類的抽象基類,完成將字節(jié)寫入流中 public abstract void write(int b) throws IOException public void write(byte b) throws IOE

4、xception public void flush() throws IOException public void close() throws IOExceptionq重要方法:數(shù)據(jù)流數(shù)據(jù)流FileInputStreamqInputStream的派生類,完成從文件中讀取字節(jié)數(shù)據(jù) q重要方法:public FileInputStream(String name) throws FileNotFoundExceptionpublic FileInputStream(File file) throws FileNotFoundException數(shù)據(jù)流數(shù)據(jù)流使用FileInputStream基本

5、步驟:q建立文件輸入流 FileInputStream in=new FileInputStream(c:demo.txt);q讀入字節(jié)數(shù)據(jù)(通常使用循環(huán))while( (i=in.read() != -1 ) s=s+(char)i;q關(guān)閉in.close();在調(diào)用I/O流的方法時(shí),注意捕捉異常如IOException數(shù)據(jù)流數(shù)據(jù)流FileOutputStreamqOutputStream的派生類,完成將字節(jié)數(shù)據(jù)寫入文件中q重要方法:public FileOutputStream(String name) throws FileNotFoundExceptionpublic FileOutp

6、utStream(String name, boolean append) throws FileNotFoundException數(shù)據(jù)流數(shù)據(jù)流使用FileOutputStream基本步驟:1. 建立文件輸出流 2. 寫字節(jié)數(shù)據(jù) 3. 關(guān)閉try FileOutputStream out=new FileOutputStream(file); String content=Wish give you better future; char ch=content.toCharArray(); /轉(zhuǎn)換成字符數(shù)組 for(int i=0;ich.length;i+) /循環(huán)將字符寫入文件 out.w

7、rite(chi); out.close(); /關(guān)閉文件流,否則文件中沒內(nèi)容 catch(Exception e) System.out.println(寫入文件時(shí)出錯(cuò));操作流操作流q高層流,不從I/O設(shè)備中讀取或?qū)懭霐?shù)據(jù),僅從其它流中讀取或?qū)懭霐?shù)據(jù),提供了普通流所沒有提供的方法來簡化編程,或提高I/O的效率 q操作流類利用了“裝飾器”設(shè)計(jì)模式,本身繼承了InputStream類,加入了大量的方法,用來“裝飾”其它數(shù)據(jù)流類,避免了大量創(chuàng)建新的功能類 操作流操作流qBufferedInputStreamq一個(gè)帶緩沖區(qū)的輸入流 q重要方法:public BufferedInputStream(

8、InputStream) public BufferedInputStream(InputStream, int) 操作流操作流qBufferedOutputStreamq一個(gè)帶緩沖區(qū)的輸出流 q重要方法:public BufferedOutputStream(OutputStream) public BufferedOutputStream(OutputStream, int) 操作流操作流qPrintStreamq提供了print和println方法,使輸出不再被限制于單個(gè)字節(jié)。且?guī)в芯彌_區(qū) q重要方法:public PrintStream(OutputStream out) public

9、 PrintStream(OutputStream out ,boolean autoFlush) public void flush()public void println(類型 x) 操作流操作流 import java.io.*; public class T24public static void main(String args)try /創(chuàng)建帶有緩沖的字節(jié)輸入流 BufferedInputStream bi = new BufferedInputStream(System.in); String s=; int i; /利用循環(huán)讀取鍵盤上的輸入,并將其轉(zhuǎn)換成字符判斷是否不為回車

10、while ( (char)(i = bi.read() !=n) s = s +(char)i; /創(chuàng)建字節(jié)打印輸出流,套接系統(tǒng)默認(rèn)輸出流PrintStream out=new PrintStream(System.out); out.println(s);catch(Exception e) System.out.println(I/O錯(cuò)誤); 字符流字符流q數(shù)據(jù)流(底層流): ReaderWriterFileReaderFileWriterq操作流(高層流):BufferedReaderBufferedWriterPrintWriter數(shù)據(jù)流數(shù)據(jù)流Readerq字符輸入流的抽象基類 q重

11、要方法public int read() throws IOExceptionpublic abstract int read(char cbuf, int off, int len) throws IOExceptionpublic abstract void close() throws IOException數(shù)據(jù)流數(shù)據(jù)流Writerq字符輸出流的抽象基類 q重要方法public void write(int c) throws IOExceptionpublic abstract void write(char cbuf, int off, int len) throws IOExcep

12、tionpublic abstract void flush() throws IOExceptionpublic abstract void close() throws IOException數(shù)據(jù)流數(shù)據(jù)流FileReaderq完成基本的文件I/O訪問。能以字符方式從文件中讀數(shù)據(jù) q重要方法public FileReader(String fileName) throws FileNotFoundExceptionpublic FileReader(File file) throws FileNotFoundException數(shù)據(jù)流數(shù)據(jù)流FileWriterq使用缺省字符編碼構(gòu)造文件流,向文

13、件輸出字符數(shù)據(jù)。如果文件不存在則創(chuàng)建該文件;如果該文件已存在,則缺省為覆蓋該文件 public FileWriter(String fileName) throws IOExceptionpublic FileWriter(String fileName, boolean append) throws IOExceptionpublic FileWriter(File file) throws IOException數(shù)據(jù)流數(shù)據(jù)流q示例FileReader in=new FileReader(file); /建立字符文件輸入流,可顯示中文while(i=in.read()!=-1) /循環(huán)依次讀

14、取文件中的所有字節(jié) s=s+(char)i;in.close;FileWriter out=new FileWriter(file); /建立字符文件輸出流,可輸出中文char ch=content.toCharArray();System.out.println(ch);for(int i=0;ich.length;i+) /循環(huán)依次讀取字符串中的字符寫入文件 out.write(chi);out.close(); /關(guān)閉文件流,否則文件中沒內(nèi)容操作流操作流q高層流,采用裝飾器設(shè)計(jì)模式創(chuàng)建q提供了普通流所沒有提供的方法來簡化編程,或提高I/O的效率 操作流操作流BufferedReaderq

15、從字符輸入流中讀取文本并將字符存入緩沖區(qū)以便能提供字符的高效讀取。提供了非常重要的讀取一行的方法 q重要方法public BufferedReader(Reader in)public BufferedReader(Reader in , int size) public String readLine() throws IOException操作流操作流BufferedWriterq將數(shù)據(jù)寫到緩沖區(qū)內(nèi),當(dāng)緩沖區(qū)滿時(shí),把數(shù)據(jù)寫到字符輸出流 public BufferedWriter(Writer) public BufferedWriter(Writer, int) 操作流操作流PrintWr

16、iterq將格式化對象打印到一個(gè)文本輸出流。 public PrintWriter(OutputStream)public PrintWriter(Writer) public PrintWriter(OutputStream, boolean) public PrintWriter(Writer, boolean) public void println(類型 x)特殊的特殊的I/O-字節(jié)字符橋接流字節(jié)字符橋接流InputStreamReaderq采用適配器設(shè)計(jì)模式,把InputStream類型轉(zhuǎn)換為Reader類型q重要方法 public InputStreamReader(InputSt

17、ream in)public InputStreamReader(InputStream in, String enc) 字節(jié)字符橋接流字節(jié)字符橋接流OutputStreamWriterq采用適配器設(shè)計(jì)模式,把OutputStream類型轉(zhuǎn)換為Writer類型q重要方法 public OutputStreamWriter(OutputStream in)public OutputStreamWriter(OutputStream, String) 標(biāo)準(zhǔn)標(biāo)準(zhǔn)I/O java.lang.System類中提供了三個(gè)靜態(tài)變量q in InputStream類型q out PrintStream類型q

18、err PrintStream類型標(biāo)準(zhǔn)標(biāo)準(zhǔn)I/Oq對System.in的適配轉(zhuǎn)換InputStreamReader reader = new InputStreamReader(System.in);BufferredReader br = new BufferedRader(reader);q對System.out的適配轉(zhuǎn)換PrintWriter pw = new PrintWriter(System.out, true);File類類qFile類并不在I/O的繼承結(jié)構(gòu)中,用于表示主機(jī)文件系統(tǒng)中的文件名或路徑名 q重要方法:public File(String s) public File(String path, String name)public boolean delete()public boolean exists()File類類q 示例public class IO4 public static void main(String args)File f=new File(c:demo.txt);System.out.println(f.getAbsolutePath()

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論