




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、 畢業(yè)設(shè)計(jì)(論文)翻譯題目: 軟件工程網(wǎng)上輔助教學(xué)系統(tǒng)學(xué) 院 軟件學(xué)院專業(yè)名稱 軟件工程班級(jí)學(xué)號(hào) 03201516學(xué)生姓名 劉雅寧指導(dǎo)教師 陳斌全二00七 年 六 月1中文翻譯 I/O系統(tǒng)“對(duì)語言設(shè)計(jì)人員來說,創(chuàng)建好的輸入輸出系統(tǒng)是一項(xiàng)特別困難的任務(wù)。”由于存在大量不同的設(shè)計(jì)方案,所以該任務(wù)的困難性是很容易證明的。其中最大的挑戰(zhàn)似乎是如何覆蓋所有可能的因素。不僅有三種不同的種類的IO需要考慮(文件、控制臺(tái)、網(wǎng)絡(luò)連接),而且需要通過大量不同的方式與它們通信(順序、隨機(jī)訪問、二進(jìn)制、字符、按行、按字等等)。Java庫的設(shè)計(jì)者通過創(chuàng)建大量類來攻克這個(gè)難題。事實(shí)上,Java的IO系統(tǒng)采用了如此多的類,
2、以致剛開始會(huì)產(chǎn)生不知從何處入手的感覺(具有諷刺意味的是,Java的IO設(shè)計(jì)初衷實(shí)際要求避免過多的類)。從Java 1.0升級(jí)到Java 1.1后,IO庫的設(shè)計(jì)也發(fā)生了顯著的變化。此時(shí)并非簡單地用新庫替換舊庫,Sun的設(shè)計(jì)人員對(duì)原來的庫進(jìn)行了大手筆的擴(kuò)展,添加了大量新的內(nèi)容。因此,我們有時(shí)不得不混合使用新庫與舊庫,產(chǎn)生令人無奈的復(fù)雜代碼。本文將幫助大家理解標(biāo)準(zhǔn)Java庫內(nèi)的各種IO類,并學(xué)習(xí)如何使用它們。本章的第一部分將介紹“舊”的Java 1.0 IO流庫,因?yàn)楝F(xiàn)在有大量代碼仍在使用那個(gè)庫。本章剩下的部分將為大家引入Java 1.1 IO庫的一些新特性。注意若用Java 1.1編譯器來編譯本章
3、第一部分介紹的部分代碼,可能會(huì)得到一條“不建議使用該特性”(Deprecated feature)警告消息。代碼仍然能夠使用;編譯器只是建議我們換用本章后面要講述的一些新特性。但我們這樣做是有價(jià)值的,因?yàn)榭梢愿宄卣J(rèn)識(shí)老方法與新方法之間的一些差異,從而加深我們的理解(并可順利閱讀為Java 1.0寫的代碼)。輸入流第1到第4部分演示了輸入流的創(chuàng)建與使用(盡管第4部分展示了將輸出流作為一個(gè)測(cè)試工具的簡單應(yīng)用)。1. 緩沖的輸入文件為打開一個(gè)文件以便輸入,需要使用一個(gè)FileInputStream,同時(shí)將一個(gè)String或File對(duì)象作為文件名使用。為提高速度,最好先對(duì)文件進(jìn)行緩沖處理,從而獲得
4、用于一個(gè)BufferedInputStream的構(gòu)建器的結(jié)果句柄。為了以格式化的形式讀取輸入數(shù)據(jù),我們將那個(gè)結(jié)果句柄賦給用于一個(gè)DataInputStream的構(gòu)建器。DataInputStream是我們的最終(final)對(duì)象,并是我們進(jìn)行讀取操作的接口。在這個(gè)例子中,只用到了readLine()方法,但理所當(dāng)然任何DataInputStream方法都可以采用。一旦抵達(dá)文件末尾,readLine()就會(huì)返回一個(gè)null(空),以便中止并退出while循環(huán)。 “String s2”用于聚集完整的文件內(nèi)容(包括必須添加的新行,因?yàn)閞eadLine()去除了那些行)。隨后,在本程序的后面部分中使用
5、s2。最后,我們調(diào)用close(),用它關(guān)閉文件。從技術(shù)上說,會(huì)在運(yùn)行finalize()時(shí)調(diào)用close()。而且我們希望一旦程序退出,就發(fā)生這種情況(無論是否進(jìn)行垃圾收集)。然而,Java 1.0有一個(gè)非常突出的錯(cuò)誤(Bug),造成這種情況不會(huì)發(fā)生。在Java 1.1中,必須明確調(diào)用System.runFinalizersOnExit(true),用它保證會(huì)為系統(tǒng)中的每個(gè)對(duì)象調(diào)用finalize()。然而,最安全的方法還是為文件明確調(diào)用close()。2. 從內(nèi)存輸入這一部分采用已經(jīng)包含了完整文件內(nèi)容的String s2,并用它創(chuàng)建一個(gè)StringBufferInputStream(字串緩
6、沖輸入流)作為構(gòu)建器的參數(shù),要求使用一個(gè)String,而非一個(gè)StringBuffer)。隨后,我們用read()依次讀取每個(gè)字符,并將其發(fā)送至控制臺(tái)。注意read()將下一個(gè)字節(jié)返回為int,所以必須將其造型為一個(gè)char,以便正確地打印。3. 格式化內(nèi)存輸入StringBufferInputStream的接口是有限的,所以通常需要將其封裝到一個(gè)DataInputStream內(nèi),從而增強(qiáng)它的能力。然而,若選擇用readByte()每次讀出一個(gè)字符,那么所有值都是有效的,所以不可再用返回值來偵測(cè)何時(shí)結(jié)束輸入。相反,可用available()方法判斷有多少字符可用。下面這個(gè)例子展示了如何從文件中
7、一次讀出一個(gè)字符:/: TestEOF.java/ Testing for the end of file while reading/ a byte at a time.importpublic class TestEOF public static void main(String args) try DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("TestEof.java"); while(in.available() != 0)char)i
8、n.readByte(); catch (IOException e) "IOException"); /:注意取決于當(dāng)前從什么媒體讀入,avaiable()的工作方式也是有所區(qū)別的。它在字面上意味著“可以不受阻塞讀取的字節(jié)數(shù)量”。對(duì)一個(gè)文件來說,它意味著整個(gè)文件。但對(duì)一個(gè)不同種類的數(shù)據(jù)流來說,它卻可能有不同的含義。因此在使用時(shí)應(yīng)考慮周全。為了在這樣的情況下偵測(cè)輸入的結(jié)束,也可以通過捕獲一個(gè)違例來實(shí)現(xiàn)。然而,若真的用違例來控制數(shù)據(jù)流,卻顯得有些大材小用。4. 行的編號(hào)與文件輸出這個(gè)例子展示了如何LineNumberInputStream來跟蹤輸入行的編號(hào)。在這里,不可簡單地
9、將所有構(gòu)建器都組合起來,因?yàn)楸仨毐3諰ineNumberInputStream的一個(gè)句柄(注意這并非一種繼承環(huán)境,所以不能簡單地將in4造型到一個(gè)LineNumberInputStream)。因此,li容納了指向LineNumberInputStream的句柄,然后在它的基礎(chǔ)上創(chuàng)建一個(gè)DataInputStream,以便讀入數(shù)據(jù)。這個(gè)例子也展示了如何將格式化數(shù)據(jù)寫入一個(gè)文件。首先創(chuàng)建了一個(gè)FileOutputStream,用它同一個(gè)文件連接。考慮到效率方面的原因,它生成了一個(gè)BufferedOutputStream。這幾乎肯定是我們一般的做法,但卻必須明確地這樣做。隨后為了進(jìn)行格式化,它轉(zhuǎn)換成
10、一個(gè)PrintStream。用這種方式創(chuàng)建的數(shù)據(jù)文件可作為一個(gè)原始的文本文件讀取。標(biāo)志DataInputStream何時(shí)結(jié)束的一個(gè)方法是readLine()。一旦沒有更多的字串可以讀取,它就會(huì)返回null。每個(gè)行都會(huì)伴隨自己的行號(hào)打印到文件里。該行號(hào)可通過li查詢。可看到用于out1的、一個(gè)明確指定的close()。若程序準(zhǔn)備掉轉(zhuǎn)頭來,并再次讀取相同的文件,這種做法就顯得相當(dāng)有用。然而,該程序直到結(jié)束也沒有檢查文件IODemo.txt。正如以前指出的那樣,如果不為自己的所有輸出文件調(diào)用close(),就可能發(fā)現(xiàn)緩沖區(qū)不會(huì)得到刷新,造成它們不完整。輸出流兩類主要的輸出流是按它們寫入數(shù)據(jù)的方式劃分
11、的:一種按人的習(xí)慣寫入,另一種為了以后由一個(gè)DataInputStream而寫入。RandomAccessFile是獨(dú)立的,盡管它的數(shù)據(jù)格式兼容于DataInputStream和DataOutputStream。5. 保存與恢復(fù)數(shù)據(jù)PrintStream能格式化數(shù)據(jù),使其能按我們的習(xí)慣閱讀。但為了輸出數(shù)據(jù),以便由另一個(gè)數(shù)據(jù)流恢復(fù),則需用一個(gè)DataOutputStream寫入數(shù)據(jù),并用一個(gè)DataInputStream恢復(fù)(獲取)數(shù)據(jù)。當(dāng)然,這些數(shù)據(jù)流可以是任何東西,但這里采用的是一個(gè)文件,并進(jìn)行了緩沖處理,以加快讀寫速度。注意字串是用writeBytes()寫入的,而非writeChars(
12、)。若使用后者,寫入的就是16位Unicode字符。由于DataInputStream中沒有補(bǔ)充的“readChars”方法,所以不得不用readChar()每次取出一個(gè)字符。所以對(duì)ASCII來說,更方便的做法是將字符作為字節(jié)寫入,在后面跟隨一個(gè)新行;然后再用readLine()將字符當(dāng)作普通的ASCII行讀回。writeDouble()將double數(shù)字保存到數(shù)據(jù)流中,并用補(bǔ)充的readDouble()恢復(fù)它。但為了保證任何讀方法能夠正常工作,必須知道數(shù)據(jù)項(xiàng)在流中的準(zhǔn)確位置,因?yàn)榧扔锌赡軐⒈4娴膁ouble數(shù)據(jù)作為一個(gè)簡單的字節(jié)序列讀入,也有可能作為char或其他格式讀入。所以必須要么為文件
13、中的數(shù)據(jù)采用固定的格式,要么將額外的信息保存到文件中,以便正確判斷數(shù)據(jù)的存放位置。6. 讀寫隨機(jī)訪問文件正如早先指出的那樣,RandomAccessFile與IO層次結(jié)構(gòu)的剩余部分幾乎是完全隔離的,盡管它也實(shí)現(xiàn)了DataInput和DataOutput接口。所以不可將其與InputStream及OutputStream子類的任何部分關(guān)聯(lián)起來。盡管也許能將一個(gè)ByteArrayInputStream當(dāng)作一個(gè)隨機(jī)訪問元素對(duì)待,但只能用RandomAccessFile打開一個(gè)文件。必須假定RandomAccessFile已得到了正確的緩沖,因?yàn)槲覀儾荒茏孕羞x擇。可以自行選擇的是第二個(gè)構(gòu)建器參數(shù):可決
14、定以“只讀”(r)方式或“讀寫”(rw)方式打開一個(gè)RandomAccessFile文件。使用RandomAccessFile的時(shí)候,類似于組合使用DataInputStream和DataOutputStream(因?yàn)樗鼘?shí)現(xiàn)了等同的接口)。除此以外,還可看到程序中使用了seek(),以便在文件中到處移動(dòng),對(duì)某個(gè)值作出修改。2英文翻譯I/O streamsCreating a good input/output (I/O) system is one of the more difficult tasks for the language designer.This is evidenced b
15、y the number of different approaches. The challenge seems to be in covering all eventualities. Not only are there different sources and sinks of I/O that you want to communicate with (files, the console, network connections, etc.), but you need to talk to them in a wide variety of ways (sequential,
16、random-access, buffered, binary, character, by lines, by words, etc.). The Java library designers attacked this problem by creating lots of classes. In fact, there are so many classes for Javas I/O system that it can be intimidating at first (ironically, the Java I/O design actually prevents an expl
17、osion of classes). There was also a significant change in the I/O library after Java 1.0, when the original byte-oriented library was supplemented with char-oriented, Unicode-based I/O classes. In JDK 1.4, the nio classes (for “new I/O,” a name well still be using years from now) were added for impr
18、oved performance and functionality. As a result, there are a fair number of classes to learn before you understand enough of Javas I/O picture that you can use it properly. In addition, its rather important to understand the evolution history of the I/O library, even if your first reaction is “dont
19、bother me with history, just show me how to use it!” The problem is that without the historical perspective, you will rapidly become confused with some of the classes and when you should and shouldnt use them. This chapter will give you an introduction to the variety of I/O classes in the standard J
20、ava library and how to use them. Input streamsParts 1 through 4 demonstrate the creation and use of input streams. Part 4 also shows the simple use of an output stream.1. Buffered input fileTo open a file for character input, you use a FileInputReader with a String or a File object as the file name.
21、 For speed, youll want that file to be buffered so you give the resulting reference to the constructor for a BufferedReader. Since BufferedReader also provides the readLine( ) method, this is your final object and the interface you read from. When you reach the end of the file, readLine( )
22、 returns null so that is used to break out of the while loop. The String s2 is used to accumulate the entire contents of the file (including newlines that must be added since readLine( ) strips them off). s2 is then used in the later portions of this program. Finally, close( ) is called to
23、 close the file. Technically, close( ) will be called when finalize( ) runs, and this is supposed to happen (whether or not garbage collection occurs) as the program exits. However, this has been inconsistently implemented, so the only safe approach is to explicitly call close( ) for
24、files. Section 1b shows how you can wrap System.in for reading console input. System.in is an InputStream, and BufferedReader needs a Reader argument, so InputStreamReader is brought in to perform the adaptation. 2. Input from memoryThis section takes the String s2 that now contains the entire conte
25、nts of the file and uses it to create a StringReader. Then read( ) is used to read each character one at a time and send it out to the console. Note that read( ) returns the next byte as an int and thus it must be cast to a char to print properly. 3. Formatted memory inputTo read “formatte
26、d” data, you use a DataInputStream, which is a byte-oriented I/O class (rather than char-oriented). Thus you must use all InputStream classes rather than Reader classes. Of course, you can read anything (such as a file) as bytes using InputStream classes, but here a String is used. To convert the St
27、ring to an array of bytes, which is what is appropriate for a ByteArrayInputStream, String has a getBytes( ) method to do the job. At that point, you have an appropriate InputStream to hand to DataInputStream. If you read the characters from a DataInputStream one byte at a time using readByte(&
28、#160;), any byte value is a legitimate result, so the return value cannot be used to detect the end of input. Instead, you can use the available( ) method to find out how many more characters are available. Heres an example that shows how to read a file one byte at a time:/: c12:TestEOF.java/ T
29、esting for end of file while reading a byte at a time.importpublic class TestEOF / Throw exceptions to console: public static void main(String args) throws IOException DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("TestEOF.java"); while(in.available
30、() != 0)char)in.readByte(); /:Note that available ( ) works differently depending on what sort of medium youre reading from; its literally “the number of bytes that can be read without blocking.” With a file, this means the whole file, but with a different kind of stream this might not be true,
31、 so use it thoughtfully. You could also detect the end of input in cases like these by catching an exception. However, the use of exceptions for control flow is considered a misuse of that feature. 4. File outputThis example also shows how to write data to a file. First, a FileWriter is created to c
32、onnect to the file. Youll virtually always want to buffer the output by wrapping it in a BufferedWriter (try removing this wrapping to see the impact on the performancebuffering tends to dramatically increase performance of I/O operations). Then for the formatting its turned into a PrintWriter. The
33、data file created this way is readable as an ordinary text file. As the lines are written to the file, line numbers are added. Note that LineNumberInputStream is not used, because its a silly class and you dont need it. As shown here, its trivial to keep track of your own line numbers. When the inpu
34、t stream is exhausted, readLine( ) returns null. Youll see an explicit close( ) for out1, because if you dont call close( ) for all your output files, you might discover that the buffers dont get flushed, so theyre incomplete. Output streamsThe two primary kinds of output streams are
35、separated by the way they write data; one writes it for human consumption, and the other writes it to be reacquired by a DataInputStream. The RandomAccessFile stands alone, although its data format is compatible with the DataInputStream and DataOutputStream. 5. Storing and recovering dataA PrintWrit
36、er formats data so that its readable by a human. However, to output data for recovery by another stream, you use a DataOutputStream to write the data and a DataInputStream to recover the data. Of course, these streams could be anything, but here a file is used, buffered for both reading and writing.
37、 DataOutputStream and DataInputStream are byte-oriented and thus require the InputStreams and OutputStreams. If you use a DataOutputStream to write the data, then Java guarantees that you can accurately recover the data using a DataInputStreamregardless of what different platforms write and read the
38、 data. This is incredibly valuable, as anyone knows who has spent time worrying about platform-specific data issues. That problem vanishes if you have Java on both platforms. When using a DataOutputStream, the only reliable way to write a String so that it can be recovered by a DataInputStream is to
39、 use UTF-8 encoding, accomplished in section 5 of the example using writeUTF( ) and readUTF( ). UTF-8 is a variation on Unicode, which stores all characters in two bytes. If youre working with ASCII or mostly ASCII characters (which occupy only seven bits), this is a tremendous waste of sp
40、ace and/or bandwidth, so UTF-8 encodes ASCII characters in a single byte, and non-ASCII characters in two or three bytes. In addition, the length of the string is stored in the first two bytes. However, writeUTF( ) and readUTF( ) use a special variation of UTF-8 for Java (which is complete
41、ly described in the JDK documentation for those methods) , so if you read a string written with writeUTF( ) using a non-Java program, you must write special code in order to read the string properly. With writeUTF( ) and readUTF( ), you can intermingle Strings and other types of data
42、using a DataOutputStream with the knowledge that the Strings will be properly stored as Unicode, and will be easily recoverable with a DataInputStream The writeDouble( ) stores the double number to the stream and the complementary readDouble( ) recovers it (there are similar methods for reading and writing the other types). But for any of the reading methods to work correctly, you must know the exact placement of the data item in the stream, since it would be equally possible t
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- java面試題及答案寫兩個(gè)線程類
- 農(nóng)村電商直播基地與農(nóng)村電商扶貧模式創(chuàng)新與實(shí)施
- 新政電工考試題及答案
- 上海微創(chuàng)java技術(shù)支持面試題及答案
- 2025年汽車發(fā)動(dòng)機(jī)項(xiàng)目提案報(bào)告模板
- 院內(nèi)醫(yī)生急救培訓(xùn)
- 幼兒園保育員接待培訓(xùn)
- 員工薪酬培訓(xùn)課件
- 中班健康:講衛(wèi)生防生病
- 秩序主管培訓(xùn)計(jì)劃
- 各專業(yè)試驗(yàn)報(bào)告氣味及
- 中國糖尿病腎臟病防治指南(2021年版)
- 計(jì)算機(jī)技術(shù)前沿總結(jié)課件
- 輸電線路風(fēng)偏計(jì)算基本方法
- 馬鞍山市潔源環(huán)保有限公司馬鞍山市一般工業(yè)固廢填埋場(chǎng)項(xiàng)目重新報(bào)批環(huán)境影響報(bào)告書
- 通信線路投標(biāo)文件
- 集結(jié)號(hào)觀后感 集結(jié)號(hào)觀后感500字(最全)
- (完整版)全國各省份城市明細(xì)表
- 《“將軍飲馬”問題》說課稿
- GB/T 6109.20-2008漆包圓繞組線第20部分:200級(jí)聚酰胺酰亞胺復(fù)合聚酯或聚酯亞胺漆包銅圓線
- 《社會(huì)主義核心價(jià)值觀》優(yōu)秀課件
評(píng)論
0/150
提交評(píng)論