中英文資料翻譯_第1頁
中英文資料翻譯_第2頁
中英文資料翻譯_第3頁
中英文資料翻譯_第4頁
中英文資料翻譯_第5頁
已閱讀5頁,還剩4頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

1、 畢業設計(論文)翻譯題目: 軟件工程網上輔助教學系統學 院 軟件學院專業名稱 軟件工程班級學號 03201516學生姓名 劉雅寧指導教師 陳斌全二00七 年 六 月1中文翻譯 I/O系統“對語言設計人員來說,創建好的輸入輸出系統是一項特別困難的任務。”由于存在大量不同的設計方案,所以該任務的困難性是很容易證明的。其中最大的挑戰似乎是如何覆蓋所有可能的因素。不僅有三種不同的種類的IO需要考慮(文件、控制臺、網絡連接),而且需要通過大量不同的方式與它們通信(順序、隨機訪問、二進制、字符、按行、按字等等)。Java庫的設計者通過創建大量類來攻克這個難題。事實上,Java的IO系統采用了如此多的類,

2、以致剛開始會產生不知從何處入手的感覺(具有諷刺意味的是,Java的IO設計初衷實際要求避免過多的類)。從Java 1.0升級到Java 1.1后,IO庫的設計也發生了顯著的變化。此時并非簡單地用新庫替換舊庫,Sun的設計人員對原來的庫進行了大手筆的擴展,添加了大量新的內容。因此,我們有時不得不混合使用新庫與舊庫,產生令人無奈的復雜代碼。本文將幫助大家理解標準Java庫內的各種IO類,并學習如何使用它們。本章的第一部分將介紹“舊”的Java 1.0 IO流庫,因為現在有大量代碼仍在使用那個庫。本章剩下的部分將為大家引入Java 1.1 IO庫的一些新特性。注意若用Java 1.1編譯器來編譯本章

3、第一部分介紹的部分代碼,可能會得到一條“不建議使用該特性”(Deprecated feature)警告消息。代碼仍然能夠使用;編譯器只是建議我們換用本章后面要講述的一些新特性。但我們這樣做是有價值的,因為可以更清楚地認識老方法與新方法之間的一些差異,從而加深我們的理解(并可順利閱讀為Java 1.0寫的代碼)。輸入流第1到第4部分演示了輸入流的創建與使用(盡管第4部分展示了將輸出流作為一個測試工具的簡單應用)。1. 緩沖的輸入文件為打開一個文件以便輸入,需要使用一個FileInputStream,同時將一個String或File對象作為文件名使用。為提高速度,最好先對文件進行緩沖處理,從而獲得

4、用于一個BufferedInputStream的構建器的結果句柄。為了以格式化的形式讀取輸入數據,我們將那個結果句柄賦給用于一個DataInputStream的構建器。DataInputStream是我們的最終(final)對象,并是我們進行讀取操作的接口。在這個例子中,只用到了readLine()方法,但理所當然任何DataInputStream方法都可以采用。一旦抵達文件末尾,readLine()就會返回一個null(空),以便中止并退出while循環。 “String s2”用于聚集完整的文件內容(包括必須添加的新行,因為readLine()去除了那些行)。隨后,在本程序的后面部分中使用

5、s2。最后,我們調用close(),用它關閉文件。從技術上說,會在運行finalize()時調用close()。而且我們希望一旦程序退出,就發生這種情況(無論是否進行垃圾收集)。然而,Java 1.0有一個非常突出的錯誤(Bug),造成這種情況不會發生。在Java 1.1中,必須明確調用System.runFinalizersOnExit(true),用它保證會為系統中的每個對象調用finalize()。然而,最安全的方法還是為文件明確調用close()。2. 從內存輸入這一部分采用已經包含了完整文件內容的String s2,并用它創建一個StringBufferInputStream(字串緩

6、沖輸入流)作為構建器的參數,要求使用一個String,而非一個StringBuffer)。隨后,我們用read()依次讀取每個字符,并將其發送至控制臺。注意read()將下一個字節返回為int,所以必須將其造型為一個char,以便正確地打印。3. 格式化內存輸入StringBufferInputStream的接口是有限的,所以通常需要將其封裝到一個DataInputStream內,從而增強它的能力。然而,若選擇用readByte()每次讀出一個字符,那么所有值都是有效的,所以不可再用返回值來偵測何時結束輸入。相反,可用available()方法判斷有多少字符可用。下面這個例子展示了如何從文件中

7、一次讀出一個字符:/: 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"); /:注意取決于當前從什么媒體讀入,avaiable()的工作方式也是有所區別的。它在字面上意味著“可以不受阻塞讀取的字節數量”。對一個文件來說,它意味著整個文件。但對一個不同種類的數據流來說,它卻可能有不同的含義。因此在使用時應考慮周全。為了在這樣的情況下偵測輸入的結束,也可以通過捕獲一個違例來實現。然而,若真的用違例來控制數據流,卻顯得有些大材小用。4. 行的編號與文件輸出這個例子展示了如何LineNumberInputStream來跟蹤輸入行的編號。在這里,不可簡單地

9、將所有構建器都組合起來,因為必須保持LineNumberInputStream的一個句柄(注意這并非一種繼承環境,所以不能簡單地將in4造型到一個LineNumberInputStream)。因此,li容納了指向LineNumberInputStream的句柄,然后在它的基礎上創建一個DataInputStream,以便讀入數據。這個例子也展示了如何將格式化數據寫入一個文件。首先創建了一個FileOutputStream,用它同一個文件連接??紤]到效率方面的原因,它生成了一個BufferedOutputStream。這幾乎肯定是我們一般的做法,但卻必須明確地這樣做。隨后為了進行格式化,它轉換成

10、一個PrintStream。用這種方式創建的數據文件可作為一個原始的文本文件讀取。標志DataInputStream何時結束的一個方法是readLine()。一旦沒有更多的字串可以讀取,它就會返回null。每個行都會伴隨自己的行號打印到文件里。該行號可通過li查詢??煽吹接糜趏ut1的、一個明確指定的close()。若程序準備掉轉頭來,并再次讀取相同的文件,這種做法就顯得相當有用。然而,該程序直到結束也沒有檢查文件IODemo.txt。正如以前指出的那樣,如果不為自己的所有輸出文件調用close(),就可能發現緩沖區不會得到刷新,造成它們不完整。輸出流兩類主要的輸出流是按它們寫入數據的方式劃分

11、的:一種按人的習慣寫入,另一種為了以后由一個DataInputStream而寫入。RandomAccessFile是獨立的,盡管它的數據格式兼容于DataInputStream和DataOutputStream。5. 保存與恢復數據PrintStream能格式化數據,使其能按我們的習慣閱讀。但為了輸出數據,以便由另一個數據流恢復,則需用一個DataOutputStream寫入數據,并用一個DataInputStream恢復(獲?。祿?。當然,這些數據流可以是任何東西,但這里采用的是一個文件,并進行了緩沖處理,以加快讀寫速度。注意字串是用writeBytes()寫入的,而非writeChars(

12、)。若使用后者,寫入的就是16位Unicode字符。由于DataInputStream中沒有補充的“readChars”方法,所以不得不用readChar()每次取出一個字符。所以對ASCII來說,更方便的做法是將字符作為字節寫入,在后面跟隨一個新行;然后再用readLine()將字符當作普通的ASCII行讀回。writeDouble()將double數字保存到數據流中,并用補充的readDouble()恢復它。但為了保證任何讀方法能夠正常工作,必須知道數據項在流中的準確位置,因為既有可能將保存的double數據作為一個簡單的字節序列讀入,也有可能作為char或其他格式讀入。所以必須要么為文件

13、中的數據采用固定的格式,要么將額外的信息保存到文件中,以便正確判斷數據的存放位置。6. 讀寫隨機訪問文件正如早先指出的那樣,RandomAccessFile與IO層次結構的剩余部分幾乎是完全隔離的,盡管它也實現了DataInput和DataOutput接口。所以不可將其與InputStream及OutputStream子類的任何部分關聯起來。盡管也許能將一個ByteArrayInputStream當作一個隨機訪問元素對待,但只能用RandomAccessFile打開一個文件。必須假定RandomAccessFile已得到了正確的緩沖,因為我們不能自行選擇??梢宰孕羞x擇的是第二個構建器參數:可決

14、定以“只讀”(r)方式或“讀寫”(rw)方式打開一個RandomAccessFile文件。使用RandomAccessFile的時候,類似于組合使用DataInputStream和DataOutputStream(因為它實現了等同的接口)。除此以外,還可看到程序中使用了seek(),以便在文件中到處移動,對某個值作出修改。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等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論