面向對象java實現-07-2java輸入輸出流_第1頁
面向對象java實現-07-2java輸入輸出流_第2頁
面向對象java實現-07-2java輸入輸出流_第3頁
面向對象java實現-07-2java輸入輸出流_第4頁
面向對象java實現-07-2java輸入輸出流_第5頁
免費預覽已結束,剩余26頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第二十講Java輸入輸出流基本要求1理解Java中流的概念掌握字節流、字符流的使用掌握File類的使用2一、文件類File文件可以分為文本文件、隨機文件和二進制文件,都可通過File類來進行簡單操作。特點:靈活,功能多創建文件流:常用文件名或File類的對象創建文件流。文件過濾:將符合條件的文件選擇出來進行操作,通過接口FileFilter和FilenameFilter來實現。、創建臨時文件、改變文件名、刪除提供對文件進行創建文件等操作提供獲取文件信息的方法,如文件名、文件路徑和文件長度等File類的構造方法:public

File(String

pathname)public

File(String

parent,String

child)public

File(File

parent,String

child)public

File(URI

uri)(1)File類的方法1、文件對象public

String

getName(

)public

String

getPath(

)public

String

getAbsolutePath(

)public

String

getParent()public

File

getParentFile(

)2、文件操作public

boolean

renameTo(File

dest)public

boolean

delete()3、獲得文件的屬性public

long

length()public

boolean

exists()public

long

lastMoidfied()4、操作public

boolean

mkdir()public

String[

]

list()public

File[

]

listFiles();34例子:File類中常用方法使用publicclassFileExample{public

static

voidmain(String[]

agrs){Filefile

=newFile("F:\\Java\\10\\FileExample.java");System.out.println("文件或

是否存在:"

+

file.exists());System.out.println("是文件嗎:"

+

file.isFile());System.out.println("是

嗎:"

+

file.isDirectory());System.out.println("名稱:"

+ file

.getName());System.out.println("路徑:

"

+

file.getPath());System.out.println("絕對路徑:

"

+

file.getAbsolutePath());System.out.println("最后修改時間:"

+

file.lastModified());System.out.println("文件大小:"

+

file.length()+

"

字節");}}5例:File類中常用方法使用import

java.io.File;import

java.util.Date;public

class

File_ex{void

FileInformation(Filef){System.out.println(f.getName());System.out.println(f.getAbsolutePath());System.out.println(f.getParent());System.out.println(f.length());System.out.println(newDate(f.lastModified()));}void

DirectoryInformation(Filed){System.out.println(d.getName());System.out.println(d.getParent());int

i=0;String

lt[]=d.list();while(i<lt.length){System.out.println(lt[i]);i++;}}public

static

void

main(String

args[]){//File

f=new

File("D:/java/","file1.text");File

d=new

File("D:/");//File

d1=new

File("D:/java/");File_ex

fe=new

File_ex();//fe.FileInformation(f);fe.DirectoryInformation(d);//

d1.mkdir();}}二、流的概念java程序通過流來完成輸入/輸出,流是程序中各種信息

的抽象。可以處理文件、內存、網絡數據等多種數據形式。數據流是從源到目的的字節的有序序列,先進先出,像管道一樣。兩種基本流:InputStream(輸入流)和OutputStream(輸出流)6(1)流式輸入輸出的特點每個數據都必須等待排在它前面的數據讀入或送出之后才能被讀寫每次讀寫操作處理的都是序列中剩余的未讀寫數據中的第一個,而不能隨意選擇輸入輸出的位置Java中流的實現是在java.io包定義的類層次結構完成的。所以要在程序中使用流類,必須java.io包78(2)

Java的標準輸入輸出標準輸入輸出是指在命令行方式下的輸入輸出方式。Java通過System.in、System.out和System.err來實現標準輸入輸出和標準錯誤輸出。每當main方法被執行時,就自動生成System.in、System.out和System.err三個對象。System.in是字節輸入流InputStream類的一個對象,其中有

read方法從鍵盤讀入數據:public

int

read()

throws

IOExceptionpublic

int

read(byte[

]

b)

throws

IOExceptionSystem.out是流PrintStream類的一個對象,其中print和println方法向屏幕輸出數據。System.err是流PrintStream類的一個對象,用于向屏幕輸出錯誤信息9例子:注意異常處理public

class

Demo

{public

static

void

main(String

args[]){byte

buffer[

]=new

byte[200];int

i,d=0,count=0;System.out.print("Input

a

string:

");try{count=System.in.read(buffer);}catch(Exception

e){System.err.println("發生異常:"+e.toString());}for(i=0;i<=count-1;i++)System.out.print((char)buffer[i]);System.out.println(count);System.out.println("Input ten

char:

");for(i=1;i<=10;i++)try{d=System.in.read();System.out.println((char)d);}catch(Exception

e){System.err.println("發生異常:"+e.toString());}}}10(3)

Java的數據流Java的數據流都在java.io包里Java的數據流根據操作的數據流分為字節流和字符流字節流:流中的數據以8位字節為單位進行讀寫,以InputStream和OutputStream為基礎類。字符流:流中的數據以16位字節為單位進行讀寫,以Reader和Writer為基礎類。三、字節流InputStream和OutputStream分別是字節輸入流和字節輸出流的超類,查看圖。InputStream和OutputStream提供許多用于字節輸入輸出的方法,包括:數據的數據的寫入標記位置獲取數據量關閉數據流11(1)字節輸入流InputStream類的層次結構12三個基本read()方法int

read() //讀一個字節返回int

read(byte[]) //將數據讀入byte[],返回讀的字節數int

read(byte[],

int

offset,

int

length

)其它方法void

close() //關閉流。自頂向下關閉Filter

streamint

available()//返回未讀的字節數long

skip(longn) //跳過n個字節booleanmarkSupported()//測試打開的流是否支持書簽void

mark(int) //標記當前流,并建立int大小緩沖區void

reset(

) //

返回

出InputStream

常用方法13例子:字節文件輸入流--FileInputStream//

文件內容并顯示在屏幕public

class

Demo{public

static

void

main(String

args[]){try{FileInputStream

rf=newFileInputStream("D:\\mytext.txt");int

b;while((b=rf.read())!=-1)System.out.print((char)b);rf.close();}catch(IOException

ie){System.out.println(ie);}catch(Exceptione){System.out.println(e);}}}14字節輸出流OutputStream類層次1516OutputStream方法三個基本的write()方法void

write(int)//寫一個字節void

write(byte[])//寫一個字節數組voidwrite(byte[],int

offset,

intlength

)其它方法void

close(

)void

flush()//強行寫入例子:字節文件輸出流----FileOutputStream//

文件publicclassDemo{public

static

voidmain(String

args[]){try{Filefile=newFile("D:/file2.txt");FileOutputStream

wf=newFileOutputStream(file,true);String

s="向文件中寫入一個字符串";byteb[]=s.getBytes();wf.write(b);wf.close();}catch(Exception

e){e.printStackTrace();}}}17總結:FileInputStream和FileOutputStream實現了對文件的順序,以字節為單位對文件進行讀寫操作,主要有這樣幾步:創建文件輸入輸出流的對象打開文件用文件讀寫方法讀寫數據關閉數據流18四、字符流類Reader是字符輸入流的抽象超類,其提供的方法與InputStream類似,只是將基于Byte的參數改為基于Char。類Writer是字符輸出流的抽象超類,其提供的方法與OutputStream類似,只是將基于Byte的參數改為基于Char。Reader和Writer

類實現字節和字符間的自動轉換。每一個

輸入、輸出流,都有相應的Reader和Writer版本。19Reader的類層次結構20Reader的基本方法int

read();//讀單個字符int

read(char

cbuf[]);//讀字符放入數組中int

read(char

cbuf[],int

offset,

int

length);//讀字符放入數組的指定位置void

close() //關閉流。long

skip(long

n) //跳過n個字符boolean

markSupported(

)

//測試打開的流是否支持書簽void

mark(int) //標記當前流,并建立int大小緩沖區void

reset(

) //

返回

出boolean

ready() //測試當前流是否準備好進行讀21Writer的類層次結構2223Writer的基本方法int

write(int

c);//寫單個字符int

write(char

cbuf[]);//寫字符數組int

write(char

cbuf[],

int

offset,

int

length)

;int

write(String

str)

;int

write(String

str,

int

offset,

int

length)

;void

close(

)void

flush()//強行寫入24例:從鍵盤輸入文字存入文件,再讀出加上行號后打印在屏幕//字符緩沖流:BufferedReader和BufferedWriter,以緩沖區方式對數據進行輸入輸出。public

class

Demo{public

static

void

main(String

[]args){String

f="d:\\file2.txt";String

str="";int

i=0;try{BufferedReader

keyIn=new

BufferedReader(new

InputStreamReader(System.in));BufferedWriter

bw=new

BufferedWriter(new

FileWriter(f));BufferedReader

br

=

new

BufferedReader(new

FileReader(f));System.out.println("Please

input

file

text:");while(!(str=keyIn.readLine()).equals("exit")){bw.write(str,0,str.length());bw.newLine();}bw.close();while((str=br.readLine())!=null){i++;System.out.println(i+":

"+str);}}catch(IOException

e){

}}}總結:字節流與字符流的比較Reader

和InputStream以及Writer與

OutputStream定義的API類似,但操作的數據類型不同。所有的流——InputStream、OutputStream

、Reader、

Writer

在創建時自動打開;程序中可以調用close方法關閉流,否則Java運行環境的

收集器將隱含將流關閉。2526輸入輸出流類分類I/O

StreamsType

ofI/OStreamsDescriptionMemoryCharArrayReaderCharArrayWriterByteArrayInputStreamByteArrayOutputStream從/向內存數組讀寫數據.StringReaderStringWriterStringBufferInputStream從/向內存字符串讀寫數據PipePipedReaderPipedWriterPipedInputStreamPipedOutputStream實現管道的輸入和輸出FileFileReaderFileWriterFileInputStreamFileOutputStream統稱為文件流。對文件進行讀、寫操作27I/O

StreamsTypeofI/OStreamsDescriptionObjectSerializationObjectInputStreamObjectOutputStream對象的輸入、輸出DataConversionDataInputStreamDataOutputStream讀、寫基本數據類型PrintingPrintWriterPrintStream包含方便的打印方法BufferingBufferedReaderBufferedWriterBufferedInputStreamBufferedOutputStream在讀入或寫出時,對數據進行緩存,以減少I/O的次數。I/O

StreamsType

ofI/OStreamsDescriptionFilteringFilterReaderFilterWriterFilterInputStreamFilterOutputStream過濾流,在數據進行讀或寫時進行過濾。ConcatenationSequenceInputStream把多個輸入流連接成一個輸入流CountingLineNumberReaderLineNumberInputStream在讀入數據時對行記數Peeking

AheadPushbackReaderPushbackInputStream通過緩存機制,進行預讀Converting

betweenBytes

and

CharactersInputStreamReaderOutputStreamWriter按照一定的編碼/

標準將字節流轉換為字符流,或進行反向轉換。28五、RandomAccessFile類FileInputStream、FileOutputStream、FileReader和FileWriter流只能完成單一的功能,要么只能

,要么只能寫入。Java還提供了專門用來處理文件輸入輸出功能更加完善的RandomAccessFile流當想對一個文件進行讀寫操作時,就可以創建一個指向該文件的RandomAccessFile流即可,這樣既可以從這個流中獲取文件信息,也可以向這個流中寫入信息。29例子:參考P334例import

java.io.*;public

溫馨提示

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

評論

0/150

提交評論