




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第6章 數(shù)據(jù)流的運(yùn)用代亞非第6章 數(shù)據(jù)流的運(yùn)用6.1 輸入輸出方法6.2 輸入輸出流的基類6.3 6.4 數(shù)據(jù)輸入輸出流6.5 隨機(jī)存取文件6.6 文件的其它操作6.7 java中的unicode6.8 管道流6.9 對(duì)象流6.10 流的分類6.11 小結(jié)2文件程序終端文件程序網(wǎng)絡(luò)端點(diǎn)數(shù)據(jù)流起點(diǎn)終點(diǎn) 網(wǎng)絡(luò)端點(diǎn)文件,字符串存儲(chǔ)區(qū)6.1 輸入輸出方法什么是數(shù)據(jù)流 ?數(shù)據(jù)流是指所有的數(shù)據(jù)通信通道在java中有關(guān)流的操作使用java.io.*出于安全的考慮,小應(yīng)用不能實(shí)現(xiàn)文件I/O流36.1 輸入輸出方法System類管理標(biāo)準(zhǔn)輸入輸出流和錯(cuò)誤流 System.out:把輸出送到缺省的顯示(通常是顯示器
2、)System.in從標(biāo)準(zhǔn)輸入獲取輸入(通常是鍵盤) System.err把錯(cuò)誤信息送到缺省的顯示每當(dāng)main方法被執(zhí)行時(shí),就自動(dòng)生成上述三個(gè)對(duì)象46.1 輸入輸出方法public class ReadHello public static void main(String args) char inchar; System.out.println(“Enter a character:”); try inChar=(char)System.in.read(); Syste.out.println(“ “+ inChar); catch(IOException e) Sytem.out.pri
3、ntln(“Error reading from user”); 5streamIostream.class-f4.bat6.1 輸入輸出方法import java.io.*;class IOTest public statics void main(String args) try byte bArray=new byte128; System.out.println(“Enter something:”); System.in.read(bArray); System.out.print(“You entered:”); System.out.println(bArray); catch(
4、IOException ioe) System.out.println(ioe.toString(); 66.1 輸入輸出方法為什么輸入的是字符,輸出是亂碼?原因:System.out.println(bArray)輸出的是數(shù)組的地址而不是字符改進(jìn):將字符數(shù)組變換成字符串原來(lái)是:System.out.println(bArray);現(xiàn)在為:String s=new String(bArray,0); System.out.println(s);System.in是屬于BufferedInputStream類型System.out是屬于PrintStream類型System.err也是屬于Pri
5、ntStream類型76.2 輸入輸出流的基類Java中每一種流的基本功能依賴于基本類InputStream和OutputStream它們是抽象類,不能直接使用屬于InputStream類的方法有:read():從流中讀入數(shù)據(jù)skip():跳過(guò)流中若干字節(jié)數(shù)available():返回流中可用字節(jié)數(shù)mark():在流中標(biāo)記一個(gè)位置reset():返回標(biāo)記過(guò)得位置markSupport():是否支持標(biāo)記和復(fù)位操作close():關(guān)閉流86.2 輸入輸出流的基類方法read()提供了三種從流中讀數(shù)據(jù)的方法.int read():讀一個(gè)整數(shù)int read(byte b):讀多個(gè)字節(jié)到數(shù)組中int
6、read(byte,int off,int len);屬于OutputStream類的方法有:write(int b):將一個(gè)整數(shù)輸出到流中write(byte b):將數(shù)組中的數(shù)據(jù)輸出到流中write(byte b, int off,int len):將數(shù)組b中從off指定的位置開(kāi)始len長(zhǎng)度的數(shù)據(jù)輸出到流中96.2 輸入輸出流的基類flush():將緩沖區(qū)中的數(shù)據(jù)強(qiáng)制送出close():關(guān)閉流.PrintStream類println()不屬于OutputStream類,它是PrintStream類的子類,能提供復(fù)雜的輸出PrintStream類的方法有:write, flush, chec
7、kError,print, println,close.其中println可以輸出多種形式的數(shù)據(jù).例如: println(String s), println(char c)等10輸出文件輸入文件readwrite6.3 文件對(duì)象的建立 File(“temp”);對(duì)文件操作要定義文件流類用來(lái)打開(kāi)一個(gè)輸入文件類用來(lái)打開(kāi)一個(gè)輸出文件116.3 文件流的建立 in=new (fp); out=new (fp);例:文件拷貝(注意要捕獲文件異常)輸入流的參數(shù)是用于輸入的文件名輸出流的參數(shù)是用于輸出的文件名輸入流輸出流126.3 import java.io.*; class public static
8、 void main(String args) try File(); File(); fis=new (inFile); fos=new (outFile); int c; while(c=fis.read()!=-1) fos.write(c); fis.close(); fos.close();catch( e) System.out.println(: +e);catch(IOException e) System.err.println(: +e);136.3 增加緩沖區(qū)流,減少訪問(wèn)硬盤的次數(shù),提高效率文件文件流 緩沖區(qū)流 輸入流輸出流輸入緩沖區(qū)輸出緩沖區(qū)146.3 緩沖區(qū)流:Buf
9、feredInputStream和 BufferedOutputStream將它們與文件流相接 in=new (“”);BufferedInputStream bin= new BufferedInputStream(in,256) int len; byte bArray=new byte256;len=bin.read(bArray);len中得到是長(zhǎng)度, bArray中得到的是數(shù)據(jù)156.3 只有緩沖區(qū)滿時(shí),才會(huì)將數(shù)據(jù)送到輸出流.Java在輸出數(shù)據(jù)流中,當(dāng)對(duì)方尚未將數(shù)據(jù)取走時(shí),程序就會(huì)被阻塞.有時(shí)要人為地將尚未填滿的緩沖區(qū)中的數(shù)據(jù)送出,使用flush()方法.文件166.4 數(shù)據(jù)輸入輸出
10、流什么時(shí)候需要數(shù)據(jù)輸入輸出流?文件流和緩沖區(qū)流的處理對(duì)象是字節(jié)或字節(jié)數(shù)組,利用數(shù)據(jù)輸入輸出流可以實(shí)現(xiàn)對(duì)文件的不同數(shù)據(jù)類型的讀寫.DataInputStream、DataOutputStream一種較為高級(jí)的數(shù)據(jù)輸入輸出方式,除了字節(jié)和字節(jié)數(shù)組,還可以處理int,float,boolean等類型.還可以用readLine方法讀取一行信息可使用的方法: write,writeBoolean,read,readByte等176.4 數(shù)據(jù)輸入輸出流數(shù)據(jù)流的建立 fos= new (”);DataInputStream dis= new DataInputStream(fos)數(shù)據(jù)輸出流可以是一個(gè)已經(jīng)建
11、立好的輸入數(shù)據(jù)流對(duì)象,例如網(wǎng)絡(luò)的連結(jié),文件等.下面的例子顯示如何利用數(shù)據(jù)輸入輸出流往文件中寫不同類型的數(shù)據(jù)186.4 數(shù)據(jù)輸入輸出流class datainput_output public static void main(String args) throws IOException fos=new (“a.txt”); DataOutputStream dos=new DataOutputStream (fos); try dos.writeBoolean(true); dos.writeByte(byte)123); dos.writeChar(J); dos.writeDouble(
12、3.141592654); dos.writeFloat(2.7182f); dos.writeInt(1234567890); dos.writeLong(998877665544332211L); dos.writeShort(short)11223); finally dos.close(); 19Streamdatainputandoutput-f5.bat6.4 數(shù)據(jù)輸入輸出流DataInputStream dis=new DataInputStream( new (a.txt);try System.out.println(t +dis.readBoolean(); System.
13、out.println(t +dis.readByte(); System.out.println(t +dis.readChar(); System.out.println(t +dis.readDouble(); System.out.println(t +dis.readFloat(); System.out.println(t +dis.readInt(); System.out.println(t +dis.readLong(); System.out.println(t +dis.readShort();finallydis.close();206.4 數(shù)據(jù)輸入輸出流DateLin
14、e(InputStream in)(計(jì)算字符和行數(shù)) DataInputStream data=new DataInputStream(in); String currentLine; int lineCount=0; int charCount=0; while(currentLine=dataIn.readLine()!=null) +lineCount; charCount+=currentLine.length(); return (charCount/(float)lineCount);21文件目錄zip文件6.5 隨機(jī)存取文件類RandomAccessFile zip文件需要用隨機(jī)
15、方法處理文件目錄給出個(gè)文件的入口,可以隨機(jī)讀取.創(chuàng)建一個(gè)隨機(jī)文件new RandomAccessFile(“”, “r”);new RandomAccessFile(“”, “rw”);隨機(jī)文件可以同時(shí)完成讀和寫操作.22pos6.5 隨機(jī)存取文件支持隨機(jī)文件操作的方法:readXXX()或writeXXX()skipBytes();將指針鄉(xiāng)下移動(dòng)若干字節(jié)seek():將指針調(diào)到所需位置get():返回指針當(dāng)前位置length():返回文件長(zhǎng)度利用seek(long pos)方法查找隨機(jī)文件中的信息例:把若干個(gè)32位的整數(shù)寫到一個(gè)名為 “temp.dat”的文件中,然后利用seek方法,以相反
16、的順序再讀取這些數(shù)據(jù)236.5 隨機(jī)存取文件public class random_file public static void main(String args) int data_arr=12,31,56,23,27,1,43,65,4,99; try RandomAccess RandomAccessFile(“temp.dat”); for (int i=0;idata_arr.length;i+) randf.writeInt(data_arri); for(int i=data_arr.length-1;i=0;i-) randf.seek(i*4); System.out.pr
17、intln(randf.readInt(); randf.close(); catch (IOException e) System.out.println(“ error: “+e); 246.6 文件的其它操作使用文件類獲取文件的路徑信息設(shè)f是一個(gè)文件對(duì)象 File(“data”,temp.dat”);f.getName():返回文件名 temp.datf.getParent():返回文件所在目錄名 dataf.getPath():返回文件路徑 datatemp.datf.getAbsolutePath():返回絕對(duì)路 c:myprogdatatemp.dat256.6 文件的其它操作例:
18、 獲取當(dāng)前目錄下所有文件名和文件的尺寸: import java.io.*;public class public static void main(String args) File(“.”); String =(); for(int i=0;i) i); System.out.println(i+ current_(); 26在java中用unicode 表示字符和字符串DatainputStream的readLine方法, 以字節(jié)形式讀入, 以u(píng)nicode形式輸出DataInputStream不適合輸入是unicode的形式處理字符用InputStreamReader 類和Buffer
19、edReader 類(jdk1.1)byteUnicode16bit8 bit+6.7 java 中的unicode276.7 java 中的unicodeimport java.io;(從鍵盤上讀一個(gè)字符串)public class CharInput public static void main(String args) String s; throws IOException InputStreamReader ir; BufferedReader in; ir=new InputStreamReader(System.in); in=new BufferedReader(ir); S
20、tring s=in.readLine(); int i=Integer.parseInt(s); i=i*2; System.out.println(“the result is” +i); 可以將字符串轉(zhuǎn)換成整數(shù)加以運(yùn)算123286.7 java 中的unicode問(wèn)題:如果字符流不是來(lái)自本地,有可能編碼不一樣,直接讀取會(huì)讀出不正確字符處理方法:ir=new InputStreamReader(is,”8859_1”);采用的是iso8859_1編碼方式,在不同平臺(tái)之間正確轉(zhuǎn)換字符.296.7 java 中的unicodeimport java.io.*;class public stat
21、ic void main(String args) try fis=new (toyamei.txt); InputStreamReader dis=new InputStreamReader(fis); BufferedReader reader=new String s; BufferedReader(dis); while(s=reader.readLine()!=null) System.out.println(read: +s); dis.close(); catch(IOException e) 306.8 使用管道流PipedInputStream和PipedOutputStre
22、am創(chuàng)建管道流:PipedInputStream pis=new PipedInputStream();PipedOutputStream pos=new PipedOutputStream(pis);或:PipedOutputStream pos=new PipedOutputStream();PipedInputStream pis=new PipedInputStream(pos);輸出流輸入流316.8 使用管道流管道流一定是輸入輸出并用例:將數(shù)據(jù)從輸出管道進(jìn),從輸入管道出import java.io.*; class pipedstream public static void ma
23、in(String args) throws IOException byte aByteData1=123, aByteData2=111; PipedInputStream pis= new PipedInputStream(); PipedOutputStream pos= new PipedOutputStream(pis); System.out.println(PipedInputStream);32try pos.write(aByteData);pos.write(aByteData2);System.out.println(byte)pis.read();System.out
24、.println(byte)pis.read(); finally pis.close(); pos.close();6.8 使用管道流336.9 對(duì)象流在java.io包中什么是對(duì)象的持續(xù)性?能夠紀(jì)錄自己的狀態(tài)一邊將來(lái)再生的能力,叫對(duì)象的持續(xù)性什么是串行化?對(duì)象通過(guò)寫出描述自己狀態(tài)的的數(shù)值來(lái)記錄自己的過(guò)程叫串行化.什么是對(duì)象流?能夠輸入輸出對(duì)象的流.兩者什么關(guān)系?將串行化的對(duì)象通過(guò)對(duì)象輸入輸出流寫入文件或傳送到其它地方.346.9 對(duì)象流一個(gè)相關(guān)的例子:從一個(gè)源讀入一個(gè)簡(jiǎn)單的對(duì)象import ;import java.iopublic class GetString public Strin
25、g getStringFromUrl(URL inURL) InputStream in; try in =inURL.openStream(); catch(IOException ioe) System.out.printlin(“URL error;”+ioe); return null; return getString(in); 通過(guò)url得到一個(gè)字符串356.9 對(duì)象流public String getStringFromSocket(Socket inSocket) inputStream in; try in=inSocket.getInputStreamSream(); ca
26、tch(IOEception ioe) System.out.println(“Socket error:”+ioe); return null; return getString(in); 通過(guò)socket得到一個(gè)字符串366.9 對(duì)象流public String getString(inputStream inStream) String readString = new String(); DataInputStream in =new DataInputSream(inStream); char inChar; try while(true) inChar=in.readByte();
27、 readString=readString+inChar; catch(EOFException eof) System.out.println(readString); catch(IOException ioe) System.out.println(“error:”+ieo); return readString; 376.9 對(duì)象流下面的對(duì)象能讀嗎?Class testObject int x; int y; float angle; String name; public testObject(int x,int y,float angle, String name); this.
28、x=x;this.y;this.angle;=name; 這仍然是一個(gè)簡(jiǎn)單的對(duì)象386.9 對(duì)象流對(duì)象流是怎樣工作的?允許可串行化的對(duì)象在流中傳輸1.只有實(shí)現(xiàn)serializable接口的類才能被串行化public class Student implements Serializable int id;String name; int age; String department; public Student(int id, String name,int age, String department) this.id=id; =name; this.a
29、ge=age; this.department =departmernt; 396.9 對(duì)象流2. 構(gòu)造對(duì)象的輸入輸出流(將對(duì)象保存到文件中,或者通過(guò)網(wǎng)絡(luò)傳送到其他地方)相應(yīng)的類:ObjectInput對(duì)象的輸出: ObjectOutputStream相應(yīng)的方法:writeObject()對(duì)象的輸入:ObjectInputStream相應(yīng)的方法:readObject()注:jdk1.1以上版本支持對(duì)象流操作406.9 對(duì)象流對(duì)象流舉例:將Student類的一個(gè)實(shí)例寫到文件中import java.io.;import java.io.ObjectOutputStream;public clas
30、s Objectserpublic static void main(String args) Student stu=new Student(981036,“Li Ming”,16,“CSD”);try fo=new (“date.ser”); ObjectOutputStream so=new ObjectOutputStream(fo); os.writeObject(stu);so.close(); catch(Exception e) ; 416.9 對(duì)象流import java.io.FileInputStream;import java.io.ObjectInputStream;public class ObjectRecov public static void main(String args) Student stu; try fi=new (“date.ser”); ObjectInputStream si=new ObjectInputStream(fi); stu=(Student)si.readObject();si.close(); catch(Exception e) System.out.println(e); System.out.println(“ID: ”+stu.id+“name:”+ +“
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國(guó)家飾布藝品數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025至2030年中國(guó)低溫雙門食具消毒柜數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 內(nèi)科三基培訓(xùn)試題及答案
- 江蘇省南京師范大學(xué)附屬中學(xué)2024-2025學(xué)年高一上學(xué)期期末考試化學(xué)試卷(含答案)
- 河北省部分學(xué)校2024-2025學(xué)年高三下學(xué)期3月聯(lián)考思想政治試題(含答案)
- 施工類承包商部門級(jí)環(huán)境培訓(xùn)試題
- 2025年消防設(shè)施操作員之消防設(shè)備高級(jí)技能能力提升試卷A卷附答案
- 2024廣東省中考英語(yǔ)真題【原卷版】
- 采購(gòu)與項(xiàng)目執(zhí)行分包合同(2篇)
- 鋼管腳手架分包合同
- 2024湖南株洲市天元區(qū)面向社會(huì)招聘社區(qū)專職工作者集中筆試歷年典型考題及考點(diǎn)剖析附答案帶詳解
- 工資條員工工資明細(xì)表模板
- SL721-2015水利水電工程施工安全管理導(dǎo)則
- (正式版)JBT 11517-2024 刮板取料機(jī)
- 煤礦瓦斯抽采達(dá)標(biāo)暫行規(guī)定解讀俞
- 居民心理健康知識(shí)講座課件
- 前列腺特異性抗原(PSA)的檢測(cè)課件
- 教師教學(xué)能力大賽獲獎(jiǎng)?wù)n程標(biāo)準(zhǔn)-教師教學(xué)能力大賽
- 年產(chǎn)5萬(wàn)噸丙烯直接水合制備異丙醇工藝Aspen模擬
- 2024年英語(yǔ)專業(yè)四級(jí)考試真題及詳細(xì)答案
- 成語(yǔ)故事葉公好龍
評(píng)論
0/150
提交評(píng)論