面向?qū)ο蟪绦蛟O(shè)計第21講 Java的輸入與輸出流(下)_第1頁
面向?qū)ο蟪绦蛟O(shè)計第21講 Java的輸入與輸出流(下)_第2頁
面向?qū)ο蟪绦蛟O(shè)計第21講 Java的輸入與輸出流(下)_第3頁
面向?qū)ο蟪绦蛟O(shè)計第21講 Java的輸入與輸出流(下)_第4頁
面向?qū)ο蟪绦蛟O(shè)計第21講 Java的輸入與輸出流(下)_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

第21講Java的輸入與輸出流(下)字符流用戶程序有時需要讀取其他格式的數(shù)據(jù),如Unicode格式的文字內(nèi)容,Java提供了相應(yīng)的輸入輸出流:字符流。字符流中的大多數(shù)的類都能在字節(jié)流中找到相應(yīng)的類。字符流分為Reader和Writer兩個類,分別實現(xiàn)字符的輸入與輸出。

字符流提供了處理字符的輸入/輸出的方法,包括兩個抽象類Reader和Writer。字符流Reader指字符流的輸入流,用于輸入,而Writer指字符流的輸出流,用于輸出。Reader和Writer使用的是Unicode,可以對不同格式的流進行操作。從Reader和Writer派生出的子類的對象都能對Unicode字符流進行操作,由這些對象來實現(xiàn)與外設(shè)的連接。

Reader類和Writer類成員方法主要功能publicabstractvoidclose()throwsIOException關(guān)閉輸入流,并釋放占用的所有資源publicvoidmark(int

readlimit)throws

IOException在輸入流當前位置加上標記publicboolean

markSupported()測試輸入流是否支持標記(mark)publicintread()throwsIOException從輸入流中讀取一個字符publicintread(charc[])throwsIOException將輸入的數(shù)據(jù)存放在指定的字符數(shù)組publicabstractintread(charc[],int

offset,int

len)throwsIOException從輸入流中的offset位置開始讀取len個字符,并存放在指定的數(shù)組中publicvoidreset()throwsIOException將讀取位置移至輸入流標記之處publiclongskip(longn)throwsIOException從輸入流中跳過n個字節(jié)publicbooleanready()throwsIOException測試輸入流是否準備完成等待讀取Reader類常用方法成員方法主要功能publicabstractvoidclose()throwsIOException關(guān)閉輸出流,并釋放占用的所有資源publicvoidwrite(intc)throwsIOException寫一個字符publicvoidwrite(charcbuf[])throws

IOException寫一個字符數(shù)組publicabstractvoidwrite(charcbuf[],int

offset,int

len)throwsIOException將字符數(shù)組cbuf中從offset位置開始的len個字符寫到輸出流中public

voidwrite(Stringstr)throwsIOException寫一個字符串publicvoidwrite(Stringstr,int

offset,int

len)throws

IOException將字符串從offset位置開始,長度為len個字符數(shù)組的數(shù)據(jù)寫到輸出流中publicabstractvoidflush()throwsIOException寫緩沖區(qū)內(nèi)的所有數(shù)據(jù)Writer類常用方法FileReader、FileWriter類用于字符文件的輸入輸出處理,與文件數(shù)據(jù)流FileInputStream、FileOutputStream的功能相似。其構(gòu)造方法如下:publicFileReader(Filefile)throwsFileNotFoundExceptionpublicFileReader(Stringfilename)throwsFileNotFoundExceptionpublicFileWriter(Filefile)throwsIOExceptionpublicFileWriter(String

fileName,booleanappend)throwsIOExceptionFileReader從超類中繼承了read、close等方法,F(xiàn)ileWriter從超類中繼承了write、close等方法。例:FileReaderin=newFileReader("java11.txt");字符文件流FileReader和FileWriter類importjava.io.*;publicclassEx11_5{publicstaticvoidmain(Stringargs[]){

FileReader

fr;

int

ch;try{

fr=newFileReader("c:\\Ex11_5.java");

while((ch=fr.read())!=-1){

System.out.print((char)ch);}}catch(Exceptione){e.printStackTrace();}}}例11.5用FileReader一個字符一個字符地讀取整個文件。FileReader和FileWriter類以字符為單位進行輸入輸出,無法進行整行輸入與輸出,數(shù)據(jù)的傳輸效率很低。Java提供BufferedReader和BufferedWriter類以緩沖區(qū)方式進行輸入輸出,其構(gòu)造方法如下:publicBufferedReader(Readerin)publicBufferedReader(Reader

in,int

sz)publicBufferedWriter(Writerout)publicBufferedWriter(Writer

out,int

sz)BufferedReader流能夠讀取文本行,方法是readLine()。字符緩沖流BufferedReader類和BufferedWriter類字符緩沖流BufferedReader類和BufferedWriter類(續(xù))通過向BufferedReader傳遞一個Reader對象或者Reader子類對象來創(chuàng)建一個BufferedReader對象,如:BufferedReader

br=BufferedReader(newFileReader("java11.txt"));

然后再從流br中讀取java11.txt中的內(nèi)容。類似的,可以將BufferedWriter流與FileWriter流連接起來,然后通過BufferedWriter流將數(shù)據(jù)寫到目的地,例如:

FileWriter

fw=newFileWriter("java11.in");

BufferedWriter

bw=newBufferedWriter(fw);然后使用BufferedReader類的成員方法write(Stringsint

off,int

len)把字符串s寫到j(luò)ava11.in中,參數(shù)off是字符串s開始處的偏移量,len是寫入的字符長度。importjava.io.*;publicclassEx11_6{publicstaticvoidmain(Stringargs[]){

FileReader

fr;

BufferedReader

br;

int

ch;try{

fr=newFileReader("c:\\Ex11_6.java");

br=newBufferedReader(fr);

while((ch=br.read())!=-1){

System.out.print((char)ch);}}catch(Exceptione){ e.printStackTrace(); }}}例11.6在例11.5中按字符一個一個的讀取,效率比較低,現(xiàn)在應(yīng)用BufferedWriter類來優(yōu)化它,每次讀取若干個字符。RandomAccessFile類創(chuàng)建的流與前面的輸入輸出流不同,RandomAccessFile類獨立于字節(jié)流和字符流體系之外,不具有字節(jié)流和字符流的任何特性,它直接繼承自Java的基類Object。RandomAccessFile類有兩個構(gòu)造方法。RandomAccessFile(Stringname,Stringmode):參數(shù)name用來確定一個文件名,給出創(chuàng)建的流的源,也可以是目的地。參數(shù)mode用來決定創(chuàng)建流對文件的訪問權(quán)限,其值可以取r(只讀)或者rw(可讀寫)。注意沒有只寫方式(w)。RandomAccessFile(Filefile,Stringmode):參數(shù)file是一個File對象,給出創(chuàng)建流的源,也可以是目的地。參數(shù)mode與前面的含義是一致的。隨機讀寫文件成員方法主要功能publicvoidclose()throwsIOException關(guān)閉流,并釋放占用的所有資源publicvoidseek(longpos)throwsIOException查找隨機文件指針的位置publiclonglength()throwsIOException求隨機文件的字節(jié)長度publicfinaldoublereadDouble()throws

IOException隨機文件浮點數(shù)的讀取publicfinalint

readInt()throws

IOException隨機文件整數(shù)的讀取publicfinalcharreadChar()throws

IOException隨機文件字符的讀取publiclonggetFilePointer()throwsIOException獲取隨機文件指針所指的當前位置publicint

skipBytes(intn)throwsIOException隨機文件訪問跳過指定的字節(jié)數(shù)RandomAccessFile類常用方法例11.7應(yīng)用類RandomAccessFile編寫程序,實現(xiàn)將兩個磁盤文件"input1.txt"和"input2.txt"中的內(nèi)容合并,并顯示在屏幕上。

importjava.io.*;publicclassEx11_7{publicstaticvoidmain(Stringargs[]){Strings="";try{RandomAccessFilef1=newRandomAccessFile("c:/input1.txt","rw");

RandomAccessFilef2=newRandomAccessFile("c:/input2.txt","rw");s=f1.readLine()+f2.readLine();charc[]=s.toCharArray();

for(inti=0;i<s.length();i++)

System.out.print(c[i]);

System.out.println();}

catch(Exceptione){}}}importjava.awt.*;importjava.awt.event.*;importjava.io.*;publicclassEx11_8extendsWindowAdapterimplementsActionListener{ Framef; Buttonbtn;

TextArea

ta; StringfileName; publicstaticvoidmain(Stringargv[]){ newEx11_8("output.txt");} publicEx11_8(StringfileName){

this.fileName=fileName; f=newFrame(fileName);

f.addWindowListener(this);

btn=newButton("保存文件");

btn.addActionListener(this);

ta=newTextArea(10,40);

f.add(ta,BorderLayout.CENTER);

f.add(btn,BorderLayout.SOUTH); f.pack();

f.setVisible(true); }

實例1

publicvoidactionPerformed(ActionEvente){ try{

FileOutputStream

fout=newFileOutputStream(fileName);bytebuf[]=ta.getText().getBytes();

fout.write(buf);

fout.close();}catch(IOException

ioe){

System.err.println(e);} } publicvoidwindowClosing(WindowEvente){ System.exit(0);}}程序分析:程序引入了系統(tǒng)包。定義了一個主類Ex11_8,繼承了類Frame,且實現(xiàn)了事件監(jiān)聽器接口ActionListner。在主類中定義了一個構(gòu)造方法,實現(xiàn)了圖11-8的界面,并增加了事件監(jiān)聽器。方法actionPerformed()應(yīng)用文件輸出流將文本域中的文本寫到指定的文件中。windowClosing()方法實現(xiàn)關(guān)閉窗口。實例2

importjava.io.*;publicclassEx11_9{publicstaticvoidmain(Stringargs[]){try{

RandomAccessFiler

rf=newRandomAccessFile("c:\\Ex11_9.java","rw");Stringstr;longpof=0;longlof=rf.length();

rf.seek(pof);

inti=1;

while(pof<lof){

str=rf.readL

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論