Java語言第八章JavaIO系統(tǒng).ppt_第1頁
Java語言第八章JavaIO系統(tǒng).ppt_第2頁
Java語言第八章JavaIO系統(tǒng).ppt_第3頁
Java語言第八章JavaIO系統(tǒng).ppt_第4頁
Java語言第八章JavaIO系統(tǒng).ppt_第5頁
已閱讀5頁,還剩28頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、第8章Java I/O系統(tǒng),8.1I/O概述 8.2文件 8.3字節(jié)流和字符流處理 8.4標(biāo)準(zhǔn)流 8.5其它常用的流 8.6小結(jié),8.1 I/O概述,8.1.1流的概念 8.1.2Java中的輸入/輸出流,8.1.1 流的概念,大部分程序都需要輸入/輸出處理,比如從鍵盤讀取數(shù)據(jù)、向屏幕中輸出數(shù)據(jù)、從文件中讀或者向文件中寫數(shù)據(jù)、在一個(gè)網(wǎng)絡(luò)連接上進(jìn)行讀寫操作等。在Java中,把這些不同類型的輸入、輸出源抽象為流(Stream),而其中輸入或輸出的數(shù)據(jù)則稱為數(shù)據(jù)流(Data Stream),用統(tǒng)一的接口來表示,從而使程序設(shè)計(jì)簡(jiǎn)單明了。,8.1.2Java中的輸入/輸出流,流一般分為輸入流(Input

2、 Stream)和輸出流(Output Stream)兩類,但這種劃分并不是絕對(duì)的。比如一個(gè)文件,當(dāng)向其中寫數(shù)據(jù)時(shí),它就是一個(gè)輸出流;當(dāng)從其中讀取數(shù)據(jù)時(shí),它就是一個(gè)輸入流。當(dāng)然,鍵盤只是一個(gè)輸入流,而屏幕則只是一個(gè)輸出流。 在Java開發(fā)環(huán)境中,主要是由包java.io中提供的一系列的類和接口來實(shí)現(xiàn)輸入/輸出處理。標(biāo)準(zhǔn)輸入/輸出處理則是由包java.lang中提供的類來處理的,但這些類又都是從包java.io中的類繼承而來。 輸入流:數(shù)據(jù)提供者,可從中讀取數(shù)據(jù)出來 輸出流:數(shù)據(jù)接收者,可往其中寫數(shù)據(jù),8.2文件,8.2.1File類 8.2.2文件輸入輸出流 8.2.3讀寫文件中的基本數(shù)據(jù)類型

3、 8.2.4隨機(jī)文件的讀取,8.2.1File類,File(String pathname) File f=new File(“c:datatemp.dat”); File f=new File(“data temp.dat”); File f=new File(“temp.dat”); File(String parent, String child) File f=new File(“c:data” ,“temp.dat”); File f=new File(“data ” ,“ temp.dat”); File(File parent, String child) File f=new

4、File(new File(“c:data”) ,“temp.dat”); File f=new File(new File(“data ”) ,“ temp.dat”);,8.2.1File類,boolean canRead() boolean canWrite() boolean setReadOnly() boolean exists() boolean isDirectory() boolean isFile() boolean isHidden() long lastModified() boolean setLastModified(long time) long length()

5、,String list() String list(FilenameFilter filter) File listFiles() File listFiles(FileFilter filter) File listFiles(FilenameFilter filter) static File listRoots() boolean mkdir() boolean mkdirs() (粉色的方法在JDK1.2之后才支持),8.2.1File類,boolean createNewFile() static File createTempFile(String prefix, String

6、suffix) static File createTempFile(String prefix, String suffix, File directory) boolean delete() void deleteOnExit() boolean renameTo(File dest),8.2.1File類,String getName() File getParentFile() String getParent() String getPath() boolean isAbsolute() File getAbsoluteFile() String getAbsolutePath()

7、File getCanonicalFile() String getCanonicalPath(),8.2.1File類,8.2.2文件輸入輸出流,FileInputStream類和FileOutputStream類的構(gòu)造函數(shù)是創(chuàng)建一個(gè)輸入輸出的對(duì)象,通過引用該對(duì)象的讀寫方法,來完成對(duì)文件的輸入輸出操作。在構(gòu)造函數(shù)中,需要指定與所創(chuàng)建的輸入輸出對(duì)象相連接的文件。當(dāng)然,要構(gòu)造一個(gè)FileInputStream對(duì)象,所連接的文件必須存在而且是可讀的;構(gòu)造一個(gè)FileOutputStream對(duì)象如果輸出文件已經(jīng)存在且可寫,該文件內(nèi)容會(huì)被新的輸出所覆蓋。,8.2.3讀寫文件中的基本數(shù)據(jù)類型,boole

8、an readBoolean() Byte readByte() Char readChar() double readDouble() float readFloat() int readInt() Long readLong() short readShort() int readUnsignedByte() int readUnsignedshort() Void readFully(byte b) Void readFully(byte b,int off,int len) int skipBytes(int n)String readUTF(),DataInputStream類的讀方

9、法,8.2.3讀寫文件中的基本數(shù)據(jù)類型,void writeBoolean(Boolean b) void writeByte(int v) void writeBytes(String s) void writeChar(int v) void writeChars(String s) void writeDouble(double d) void writeFloat(float f) void writeInt(int v) void writeLong(int v) void writeShort(int v) void writeUTF(String str),DataOutputS

10、tream類的寫方法,在生成一個(gè)隨機(jī)文件對(duì)象時(shí),除了要指明文件對(duì)象和文件名之外,還需要指明訪問文件的模式。 RandomAccessFile(File file, String mode) RandomAccessFile(String name, String mode) mode 的取值: “r” 只讀. 任何寫操作都將拋出IOException。 “rw” 讀寫. 文件不存在時(shí)會(huì)創(chuàng)建該文件,文件存在時(shí),原文件內(nèi)容不變,通過寫操作改變文件內(nèi)容。 “rws” 同步讀寫. 等同于讀寫,但是任何寫操作的內(nèi)容都被直接寫入物理文件,包括文件內(nèi)容和文件屬性。 “rwd” 數(shù)據(jù)同步讀寫. 等同于讀寫,但

11、任何內(nèi)容寫操作都直接寫到物理文件,但對(duì)文件屬性內(nèi)容的修改不是這樣。,8.2.4隨機(jī)文件的讀取,對(duì)于FileInputStream/FileOutputStream、FileReader/FileWriter來說,它們的實(shí)例都是順序訪問流,即只能進(jìn)行順序讀/寫。而類RandomAccessFile則允許對(duì)文件內(nèi)容同時(shí)完成讀和寫操作,它直接繼承object,并且同時(shí)實(shí)現(xiàn)了接口DataInput和DataOutput,提供了支持隨機(jī)文件操作的方法: readXXX()或writeXXX(): 如ReadInt(), ReadLine(), WriteChar(), WriteDouble()等。 i

12、nt skipBytes(int n):將指針鄉(xiāng)下移動(dòng)若干字節(jié) length():返回文件長(zhǎng)度 long getFilePointer():返回指針當(dāng)前位置 void seek(long pos):將指針調(diào)到所需位置,8.2.4隨機(jī)文件的讀取,File f = new File(“file.txt”); new RandomAccessFile(f, “r”); new RandomAccessFile(f, “rw”); new RandomAccessFile(“file1.txt”, “r”); new RandomAccessFile(“file2.txt”, “rw”);,8.2.4

13、隨機(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 RandomAccessFile randf=new RandomAccessFile(“temp.dat”); for (int i=0; i=0; i-) randf.seek(i*4L); /int數(shù)據(jù)占4個(gè)字節(jié) System.out.println(randf.readInt(); randf.close(); catch (IOExcep

14、tion e) System.out.println(“File access error: “+e); ,8.2.4隨機(jī)文件的讀取,8.3字節(jié)流和字符流處理,8.3.1字節(jié)流 8.3.2字符流 8.3.3InputStreamReader類和OutputStreamWriter類 8.3.4BufferedReader類和BufferedWriter類,8.3.1字節(jié)流,基本字節(jié)輸入流InputStream public abstract int read() thows IOException public int read(byte b) throws IOException publi

15、c int read(byte b,int offset,int length)throws IOException public int available() throws IOException public long skip(long n) throws IOException,8.3.2字符流,關(guān)閉流 public void close() throws IOException 使用輸入流中的標(biāo)記 public mark(int readlimit) public void reset() public boolean markSupported(),8.3.2字符流,基本輸入字符

16、流Reader 讀取字符 public int read() throws IOException public int read(char chbuf,int offset ,int length) throws IOException public int read(char chbuf) throws IOException 標(biāo)記流 public Boolean markSupported() public void mark(int readAheadLimit)throws IOException public void reset() throws IOException 關(guān)閉流

17、public abstract void close() throws IOException,8.3.2字符流,基本輸出字符流Writer 向輸出流寫入字符 public void write(int a) throws IOException public void write(char chbuf) throws IOException public abstract void write(char chbuf,int offset,int length) throws IOException public void write(String str) throws IOExceptio

18、n public void write(String str,int offset,int length) throws IOException 刷新 public abstract void flush() 關(guān)閉流 public abstract void close() throws IOException,8.3.3InputStreamReader類和OutputStreamWriter類,InputStreamReader 類的主要構(gòu)造函數(shù) public InputStreamReader(InputStream in) public InputStreamReader(Intput

19、Stream in,String code) throws UnSupportedEncodingException OutputStreamWriter類的兩個(gè)主要的構(gòu)造函數(shù) public OutputStreamWriter(OutputStream out) public OutputStreamWriter(OutputStream out,String code) throws UnSupportedEncodingExceptioon,8.3.3InputStreamReader類和OutputStreamWriter類,InputStreamReader類和OutputStrea

20、mWriter類的方法 讀入和寫出字符 獲取當(dāng)前編碼方式 關(guān)閉流,8.3.4BufferedReader類和BufferedWriter類,BufferReader類 public BufferedReader(Reader in) public BufferedReader(Reader in,int bufSize) public String readLine() throws IOException,8.3.4BufferedReader類和BufferedWriter類,BufferWriter類 public BufferedWriter(Writer out) public Bu

21、fferedWriter(Writer out,int bufSize) public void newLine() throws IOException,8.4標(biāo)準(zhǔn)流,標(biāo)準(zhǔn)輸入 標(biāo)準(zhǔn)輸入System.in作為InputStream類的一個(gè)實(shí)例來實(shí)現(xiàn),可以使用read()和skip(long n)兩個(gè)方法。read()實(shí)現(xiàn)從輸入中讀一個(gè)字節(jié),skip(long n)實(shí)現(xiàn)在輸入中跳過n個(gè)字節(jié)。 標(biāo)準(zhǔn)輸出 標(biāo)準(zhǔn)輸出System.out是類System的數(shù)據(jù)成員out,它屬于PrintStream類。PrintStream類和OutputStream類的關(guān)系是:OutputStream類是一個(gè)抽象類,F(xiàn)ilterOutputStream是由抽象類OutputStream派生的,PrintStream類又是這個(gè)抽象類FilterOutputStream的一個(gè)子類。,8.5其它常用的流,管道流 構(gòu)造方法中連接 PipedInputStream(PipedOutputStream send); PipedOutputStr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論