java教程8 文件操作.ppt_第1頁
java教程8 文件操作.ppt_第2頁
java教程8 文件操作.ppt_第3頁
java教程8 文件操作.ppt_第4頁
java教程8 文件操作.ppt_第5頁
已閱讀5頁,還剩51頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、第8章,文件操作,Sunny Liu weiliu_,本課教學(xué)內(nèi)容,Java中的流 抽象流 流類 Readers和Writers 文件的輸入和輸出,問題陳述,應(yīng)用場景 為員工登錄系統(tǒng)創(chuàng)建一個登錄程序。該程序的功能說明如下: 接收用戶名和密碼 用戶情況(如用戶名、密碼、登錄日期和登錄時間)應(yīng)該被記入日志文件,流的概念,流是程序和外界進行數(shù)據(jù)交換的通道。 流是有序的數(shù)據(jù)序列,它有源(輸入流)和目的(輸出流)。 兩種基本的流是:輸入流(InputStream)和輸出流(OutputStream)。程序通過輸入流從數(shù)據(jù)源讀取數(shù)據(jù),通過輸出流向目的地寫數(shù)據(jù)。 java.io包使用術(shù)語流定義I/O(輸入和

2、輸出)。該包有兩個主要的部分:字符流(Character stream)和字節(jié)流(Byte stream)。,流的分類,字節(jié)流被稱為輸入流或輸出流,而字符流被稱為Reader或Writer。 節(jié)點流:從特定的地方讀寫的流類,例如:磁盤或一塊內(nèi)存區(qū)域。 過濾流:使用節(jié)點流作為輸入或輸出。過濾流是使用一個已經(jīng)存在的輸入流或輸出流連接創(chuàng)建的。,流的分類,抽象流,輸入流 InputStream 輸出流 OutputStream,輸入流InputStream,InputStream類是所有輸入流的父類,它是一個抽象類,不能被實例化。它提供了一系列和讀取數(shù)據(jù)有關(guān)的方法。 InputStream類最重要的方

3、法是讀取數(shù)據(jù)的read()方法。 read()方法的功能是逐字逐句地以二進制的原始方式讀數(shù)據(jù)。,輸入流InputStream,有三個基本的讀方法: abstract int read():讀取一個字節(jié)數(shù)據(jù),并返回讀到的數(shù)據(jù),如果返回-1,表示讀到了輸入流的末尾。 int read(byte b):將數(shù)據(jù)讀入一個字節(jié)數(shù)組,同時返回實際讀取的字節(jié)數(shù)。如果返回-1,表示讀到了輸入流的末尾。 int read(byte b, int off, int len):將數(shù)據(jù)讀入一個字節(jié)數(shù)組,同時返回實際讀取的字節(jié)數(shù)。如果返回-1,表示讀到了輸入流的末尾。off指定在數(shù)組b中存放數(shù)據(jù)的起始偏移位置;len指定

4、讀取的最大字節(jié)數(shù)。,輸入流InputStream,其它方法 long skip(long n):在輸入流中跳過n個字節(jié),并返回實際跳過的字節(jié)數(shù)。 int available():返回在不發(fā)生阻塞的情況下,可讀取的字節(jié)數(shù)。 void close():關(guān)閉輸入流,釋放和這個流相關(guān)的系統(tǒng)資源。 void mark(int readlimit):在輸入流的當(dāng)前位置放置一個標(biāo)記,如果讀取的字節(jié)數(shù)多于readlimit設(shè)置的值,則流忽略這個標(biāo)記。 void reset():將位置指針返回到標(biāo)記的位置。 boolean markSupported():判斷流是否支持標(biāo)記和復(fù)位操作。,java.io包中Inp

5、utStream的類層次,基本的輸入流類,輸出流OutputStream,OutputStream類是所有輸出流的父類,它是一個抽象類,不能被實例化。它提供了一系列和寫數(shù)據(jù)有關(guān)的方法。 OutputStream類最重要的方法是寫入數(shù)據(jù)的write()方法。 write()方法的功能是將字節(jié)寫入流中。,輸出流OutputStream,三個基本的寫方法 void write(int b) :往輸出流中寫入一個字節(jié)。向輸出流寫數(shù)據(jù)。 void write(byte b):往輸出流中寫入數(shù)組b中的所有字節(jié)。 void write(byte b,int off,int len):往輸出流中寫入數(shù)組b中從

6、偏移量off開始的len個字節(jié)的數(shù)據(jù)。,輸出流OutputStream,其它方法 void close():當(dāng)完成寫操作后,應(yīng)該關(guān)閉輸出流。 void flush():OutputStream類本身的flush()方法不執(zhí)行任何操作,它的一些帶有緩沖區(qū)的子類(比如BufferedOutputStream和PrintStream類)覆蓋了flush()方法。通過帶緩沖區(qū)的輸出流寫數(shù)據(jù)時,數(shù)據(jù)先保存在緩沖區(qū)中,積累到一定程度才會真正寫到輸出流中。緩沖區(qū)通常用字節(jié)數(shù)組實現(xiàn),實際上是指一塊內(nèi)存空間。flush()方法強制把緩沖區(qū)內(nèi)的數(shù)據(jù)寫到輸出流中。,java.io包中OutputStream的類層次

7、,流類,FileInputStream和FileOutputStream BufferedInputStream和BufferedOutputStream DataInputStream和DataOutputStream PipedInputStream和PipedOutputStream ByteArrayInputStream和ByteArrayOutputStream,FileInputStream和FileOutputStream類,這些流被分類為node(節(jié)點)流,是因為它們從磁盤文件讀和寫數(shù)據(jù)。 FileInputStream類允許你以流的形式從文件讀輸入;FileOutputSt

8、ream類允許你把輸出寫進流文件。 例子 FileInputStream inputFile = new FileInputStream(“Empolyee.dat”); FileOutputStream outputFile = new FileOutputStream(“bonus.dat”);,BufferedInputStream和BufferedOutputStream類,BufferedInputStream類為輸入流創(chuàng)建和維持緩沖,它用于增加輸入操作的有效性。緩沖過程由從流里一字節(jié)一字節(jié)地讀出數(shù)據(jù)來完成。 BufferedOutputStream類為輸出流創(chuàng)建和維持緩沖。 兩者都

9、表示filter(過濾器)流。,過濾輸入流:FilterInputStream,FilterInputStream是一種用于擴展輸入流功能的裝飾器,它有好幾個子類,分別用來擴展輸入流的某一種功能。,DataInputStream和DataOutputStream類,DataInputStream和DataOutputStream類創(chuàng)建的對象被稱為數(shù)據(jù)輸入流和數(shù)據(jù)輸出流。 它們不同于前面介紹的所有流,它是一種較為高級的數(shù)據(jù)輸入輸出方式。 它們允許程序按照與機器無關(guān)的風(fēng)格讀取Java的原始數(shù)據(jù)類型。,DataInputStream和DataOutputStream類,DataInputStream

10、類 將創(chuàng)建的數(shù)據(jù)輸入流指向一個由參數(shù)in指定的輸入流,以便從中讀取數(shù)據(jù)(與機器無關(guān)的風(fēng)格讀取),如: FileInputStream fis = new FileInputStream(“datafile.txt”); DataInputStream in_data = new DataInputStream(fis); DataOutputStream類 將創(chuàng)建的數(shù)據(jù)輸出流指向一個由參數(shù)out指定的輸出流,然后通過這個數(shù)據(jù)輸出流把Java數(shù)據(jù)類型的數(shù)據(jù)寫到輸出流out。 FileOutputStream fos = new FileOutputStream(“datafile.txt”);

11、DataOutputStream out_data = new DataOutputStream(fos);,示例,FileOutputStream out1=new FileOutputStream(D:test.txt); BufferedOutputStream out2=new BufferedOutputStream(out1); /裝飾文件輸出流 DataOutputStream out=new DataOutputStream(out2); /裝飾帶緩沖輸出流 out.writeByte(-12); out.writeLong(12); out.writeChar(1); out

12、.writeUTF(好); out.close(); InputStream in1=new FileInputStream(D:test.txt); BufferedInputStream in2=new BufferedInputStream(in1); /裝飾文件輸入流 DataInputStream in=new DataInputStream(in2); /裝飾緩沖輸入流 System.out.print(in.readByte()+ ); System.out.print(in.readLong()+ ); System.out.print(in.readChar()+ ); Sy

13、stem.out.print(in.readUTF()+ ); in.close();,PipedInputStream和PipedOutputStream類,管道流用于在線程之間傳輸數(shù)據(jù)。在線程里的PipedInputStream對象從出現(xiàn)在另一個線程里的PipedOutputStream對象接收輸入。這些類在創(chuàng)建管道時一起使用。 通過給PipedInputStream對象引用輸出管道,你能把輸入(inPipe)和輸出(outPipe)連接到流里。,outPipe寫進管道,inPipe從管道讀出,Readers和Writers,簡介 輸入和輸出流的子類以字節(jié)的形式讀和寫數(shù)據(jù)。Reader和W

14、riter類是抽象類支持Unicode字符流的讀和寫。 Unicode用于每個字符由16位表示的數(shù)據(jù)。字節(jié)流以八位數(shù)據(jù)為單位工作。,Readers和Writers,子類和方法 Reader和Writer類的最重要的子類是InputStreamReader和OutputStreamWriter。 Reader類支持InputStream類方法如:read(), skip(), mark(), markSupported(), reset() 和close()。 Writer類支持OutputStream類方法如:write(), flush()和close(),Reader類分層結(jié)構(gòu),Reade

15、r,BufferedReader,CharArrayReader,InputStreamReader,StringReader,PipedReader,FilterReader,Writer類分層結(jié)構(gòu),Writers,BufferedWriter,CharArrayReader,FilterWriter,StringWriter,PipedWrier,PrintWriter,OutputStreamWriter,從鍵盤讀,InputStreamReader和OutputStreamWriter類 InputStreamReader和OutputStreamWriter類用于在字節(jié)和Unicod

16、e字符流之間轉(zhuǎn)換數(shù)據(jù)。 InputStreamReader類轉(zhuǎn)換InputStream子類對象為Unicode字符流。 OutputStreamWriter流轉(zhuǎn)換Unicode字符輸出流為字節(jié)輸出流,從鍵盤讀,例子,import java.io.*; public class NumberInput public static void main(String args) try InputStreamReader ir; BufferedReader in; ir=new InputStreamReader(System.in); /從鍵盤接收了一個字符串的輸入,并創(chuàng)建了一個字符 輸入流的對

17、象 in=new BufferedReader(ir); String s=in.readLine();,從鍵盤讀,例子,/從輸入流in中讀入一行,并將讀取的值賦值給字符串變量s System.out.println(Input value is: +s); int i = Integer.parseInt(s);/轉(zhuǎn)換成int型 i*=2; System.out.println(Input value changed after doubled: +i); catch(IOException e) System.out.println(e); ,文件的輸入和輸出,簡介 Java支持在文件上的

18、基于流的輸入和輸出操作。 它提供類,如:File, FileDescriptor, FileInputStream和FileOutputStream等。 File類能訪問文件和目錄對象并提供操縱他們的方法 . FileDescriptor類幫助主機系統(tǒng)跟蹤正在訪問的文件. FileInputStream和FileOutputStream類提供讀和寫文件流的職能 . 用RandomAccessFile類能隨機訪問文件 . FileReader和FileWriter類支持基于Unicode的文件I/O,文件的輸入和輸出,File類 File類用于訪問文件和目錄對象。它使用主機操作系統(tǒng)的文件名約定。

19、 File類的方法允許你刪除和重命名文件。這些方法檢查文件的讀和寫許可。 你能用File類的目錄方法創(chuàng)建,刪除,重命名和說明路徑 要創(chuàng)建新的File類對象,你能用下列任何三種構(gòu)造函數(shù): File(String pathname) File(String parent,String child) File(File parent,String child),FileInputStream,FileInputStream是InputStream類的直接子類。該類提供三個構(gòu)造函數(shù): FileInputStream(File file) FileInputStream(String name) Fil

20、eInputStream(FileDescriptor fdObj) 從文件中讀入數(shù)據(jù)有兩種方式 直接利用FileInputStream類提供的read()方法來完成讀入操作。 以FileInputStream類對象為原始數(shù)據(jù)源,再加上其他功能強大的輸入流如DataInputStream流完成文件的讀入操作。,FileInputStream,import java.io.*; class FileReaderSample public static void main(String agrs)throws IOException FileInputStream in=new FileInput

21、Stream(“C:in.txt); int data; while(data=in.read()!=-1) System.out.print(data + ); in.close(); ,FileOutputStream,FileOutputStream向文件寫數(shù)據(jù),它有以下構(gòu)造方法: FileOutputStream(File file) FileOutputStream(String name) FileOutputStream(FileDescriptor fdObj) FileOutputStream(String name, boolean append) 在創(chuàng)建FileOutpu

22、tStream實例時,如果相應(yīng)的文件并不存在,會自動創(chuàng)建一個空的文件。如果參數(shù)file或name表示的文件路徑盡管存在,但是代表一個文件目錄,那么會拋出FileNotFoundException異常。 默認情況下,F(xiàn)ileOutputStream向文件寫數(shù)據(jù)時,將覆蓋文件中原有的內(nèi)容。以上第四個構(gòu)造方法提供了一個布爾類型的參數(shù)append,如果append參數(shù)為true,將在文件末尾添加數(shù)據(jù)。,FileOutputStream,import java.io.*; public class FileWriterSample public static void main(String args)

23、 try FileOutputStream fileOut = new FileOutputStream(C:out.txt); fileOut.write(你好.getBytes(); fileOut.close(); catch(FileNotFoundException e) catch(IOException e) ,從文件讀和寫,FileInputStream類 和FileOutputStream 類 FileInputStream類允許你以流的形式讀文件。你能用文件名串或File對象作為參數(shù)創(chuàng)建類對象。 FileOutputStream方法補充FileInputStream的方法。

24、FileOutputStream方法把輸出寫進文件流。,從文件讀和寫,例子 下列例子顯示在文件里的讀和寫操作。在該程序中,來自控制臺的輸入寫進稱為IOText.txt文件。程序為正確顯示文件的內(nèi)容然后刪除該文件。,從文件讀和寫,import java.lang.*; import java.io.*; public class IOTest public static void main (String args ) throws IOException FileOutputStream fOutputStream = new FileOutputStream(“C:IOTest.txt”);

25、 InputStreamReader inputStream = new InputStreamReader(System.in); BufferedReader bufferStream = new BufferedReader(inputStream); String ioString; System.out.print(“n Please enter something: “); System.out.flush(); ioString = bufferStream.readLine(); for( int i=0; i ioString.length();i+) fOutputStre

26、am.write( ioString.charAt(i) ); fOutputStream.close(); FileInputStream fInputStream = new FileInputStream(“ c:IOTest.txt “); int numberOfBytes = fInputStream.available();,System.out.println(“No. of bytes present: “+numberOfBytes); Byte inBuff = new bytenumberOfBytes; int readByte = fInputStream.read

27、(inBuff,0,numberOfBytes); System.out.println(“No. of bytes read: “ +readByte); System.out.println(“n They are: “+new String(inBuff); fInputStream.close(); System.out.println(“n Deleting”); File testFile = new File(“c:IOTest.txt”); testFile.delete(); ,隨機存取文件,定義 術(shù)語隨機存取意味著數(shù)據(jù)能在文件內(nèi)的任意位置讀或?qū)憽?RandomAccessF

28、ile類為執(zhí)行I/O用原始數(shù)據(jù)類型實現(xiàn)DataInput和DataOutput接口。RandomAccessFile類也支持讀和寫的許可,并且允許文件以只讀和讀寫方式訪問。,隨機存取文件,創(chuàng)建隨機存取文件 有兩種方法創(chuàng)建隨機存取文件用路徑名作為串或用File類對象。 RandomAccessFile (String pathname, String mode) RandomAccessFile( File name, String mode) 例如: RandomAccessFile randomFile = new RandomAccessFile(“iotest.txt” , “rw”);

29、 File file1 = new File(“iotest.txt”); RandomAccessFile randomFile = new RandomAccessFile(file1 , “rw”);,隨機存取文件,RandomAccessFile類的方法: void seek(long pos) long getFilePointer() long length(),序列化,定義 為使文件對象持久,你必須實行重要的一步:存儲文件。存儲文件對象被稱為序列化。序列化是實現(xiàn)持久性的關(guān)鍵。它能夠讓你把對象寫進流并能在以后讀出來。 序列化的實現(xiàn) JDK1.2提供java.io包的Serializ

30、able接口支持對象的序列化。 ObjectInputStream類的readObject()方法用于從流讀對象。被讀對象應(yīng)該符合適當(dāng)?shù)念惷?ObjectOutputStream類的writeObject()方法把對象寫進流,序列化,把對象寫進文件的例子,FileOutputStream file = new FileOutputStream(“c:String.dat”); ObjectOutputStream objout = new ObjectOutputStream(file); objout.writeObject(testStr); file.close();,序列化,讀對象的

31、例子,FileInputStream file = new FileInputStream(“c:String.dat”); ObjectInputStream objInput = new ObjectInputStream(file); testStr = (String) objInput.readObject(); file.close();,實例分析,任務(wù)單,實例分析,步驟1:確定需要記錄的用戶信息 User + +:+password + + Logged in: + +: + Date: 步驟2:確定記錄用戶信息所需要的技術(shù) 使用流寫入日志文件 步驟3:確定表示時間的類 Date

32、 Calendar GregorianCalendar,實例分析,步驟4:確定用于寫文件的類和變量名 類:RandomAccessFile 變量:logFile 步驟5:確定日志文件名 userlog.txt 步驟6:確定提供給日志文件的權(quán)限 讀和寫,實例分析,步驟7:確定接收用戶登錄信息的組件及其變量 步驟8:確定存儲日志文件輸入的變量 entry,實例分析,步驟9:確定表示時間的變量名 步驟10:確定何時在日志文件中建立一個輸入項 當(dāng)點擊OK按鈕時,在userlog.txt文件中建立一個用戶信息的輸入項,實例分析,步驟11:確定要處理的異常 輸入輸出異常IOException 步驟12:確

33、定異常的出錯信息 “不能寫入日志文件”,后跟異常對象,步驟13:編寫代碼記錄用戶的輸入,實例分析,public class LoginIO extends JFrame implements ActionListener JPanel panel; JLabel labelName; JLabel labelPass; JTextField textName; JPasswordField textPass; JButton butLogin, butCancel; GridLayout gl; Date date; GregorianCalendar gCal; public LoginIO() gl = new GridLayout(2, 3); panel = new JPanel(); getContentPane().add(panel); panel.setLayout(gl); labelName = new JLabel(用戶名); labelPass = new JLabel(密碼); textName = new JTextField(20); textPass = new JPasswordField(20);,butLogin = new JButton(登錄); butCancel = new JButto

溫馨提示

  • 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)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論