Java畢業(yè)設(shè)計(jì)外文翻譯(共10頁(yè))_第1頁(yè)
Java畢業(yè)設(shè)計(jì)外文翻譯(共10頁(yè))_第2頁(yè)
Java畢業(yè)設(shè)計(jì)外文翻譯(共10頁(yè))_第3頁(yè)
Java畢業(yè)設(shè)計(jì)外文翻譯(共10頁(yè))_第4頁(yè)
Java畢業(yè)設(shè)計(jì)外文翻譯(共10頁(yè))_第5頁(yè)
已閱讀5頁(yè),還剩6頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上畢業(yè)設(shè)計(jì)(論文)外文文獻(xiàn)翻譯譯文:Java I/O 系統(tǒng)對(duì)編程語(yǔ)言的設(shè)計(jì)者來(lái)說(shuō),創(chuàng)建一套好的輸入輸出(I/O)系統(tǒng),是一項(xiàng)難度極高的任務(wù)。這一類(lèi)可以從解決方案的數(shù)量之多上看出端倪。這個(gè)問(wèn)題就難在它要面對(duì)的可能性太多了。不僅是因?yàn)橛心敲炊嗟腎/O的源和目的(文件,控制臺(tái),網(wǎng)絡(luò)連接等等),而且還有很多方法(順序的,隨機(jī)的,緩存的,二進(jìn)制的,字符方式的,行的,字的等等)。Java類(lèi)庫(kù)的設(shè)計(jì)者們用“創(chuàng)建很多類(lèi)”的辦法來(lái)解決這個(gè)問(wèn)題。坦率地說(shuō),Java I/O系統(tǒng)的類(lèi)實(shí)在太多了,以至于初看起來(lái)會(huì)把人嚇著(但是,具有諷刺意味的是,這種設(shè)計(jì)實(shí)際上是限制了類(lèi)的爆炸性增長(zhǎng))。此外,Ja

2、va在1.0版之后又對(duì)其I/O類(lèi)庫(kù)進(jìn)行了重大的修改,原先是面向byte的,現(xiàn)在又補(bǔ)充了面向Unicode字符的類(lèi)庫(kù)。為了提高性能,完善功能,JDK1.4又加了一個(gè)nio(意思是“new I/O”。這個(gè)名字會(huì)用上很多年)。這么以來(lái),如果你想對(duì)Java 的I/O類(lèi)庫(kù)有個(gè)全面了解,并且做到運(yùn)用自如,你就得先學(xué)習(xí)大量的類(lèi)。此外,了解I/O類(lèi)庫(kù)的演化歷史也是相當(dāng)重要的??赡苣愕牡谝环磻?yīng)是“別拿什么歷史來(lái)煩我了,告訴我怎么用就可以了!”但問(wèn)題是,如果你對(duì)這段一無(wú)所知,很快就會(huì)被一些有用或是沒(méi)用的類(lèi)給搞糊涂了。本文會(huì)介紹Java 標(biāo)準(zhǔn)類(lèi)庫(kù)中的各種I/O類(lèi),及其使用方法。File 類(lèi)在介紹直接從流里讀寫(xiě)數(shù)據(jù)的

3、類(lèi)之前,我們先介紹一下處理文件和目錄的類(lèi)。你會(huì)認(rèn)為這是一個(gè)關(guān)于文件的類(lèi),但它不是。你可以用它來(lái)表示某個(gè)文件的名字,也可以用它來(lái)表示目錄里一組文件的名字。如果它表示的是一組文件,那么你還可以用list( )方法來(lái)進(jìn)行查詢(xún),讓它會(huì)返回String數(shù)組。由于元素?cái)?shù)量是固定的,因此數(shù)組會(huì)比容器更好一些。如果你想要獲取另一個(gè)目錄的清單,再建一個(gè)File對(duì)象就是了。 目錄列表器假設(shè)你想看看這個(gè)目錄。有兩個(gè)辦法。一是不帶參數(shù)調(diào)用list( )。它返回的是File對(duì)象所含內(nèi)容的完整清單。但是,如果你要的是一個(gè)"限制性列表(restricted list)"的話(huà) 比方說(shuō),你想看看所有擴(kuò)展名為

4、.java的文件 那么你就得使用"目錄過(guò)濾器"了。這是一個(gè)專(zhuān)門(mén)負(fù)責(zé)挑選顯示File對(duì)象的內(nèi)容的類(lèi)。 FilenameFilter接口的聲明: public interface FilenameFilter boolean accept(File dir, String name);accept( )方法需要兩個(gè)參數(shù),一個(gè)是File對(duì)象,表示這個(gè)文件是在哪個(gè)目錄里面的;另一個(gè)是String,表示文件名。雖然你可以忽略它們中的一個(gè),甚至兩個(gè)都不管,但是你大概總得用一下文件名吧。記住,list( )會(huì)對(duì)目錄里的每個(gè)文件調(diào)用accept( ),并以此判斷是不是把它包括到返回值里;這

5、個(gè)判斷依據(jù)就是accept( )的返回值。 切記,文件名里不能有路徑信息。為此你只要用一個(gè)String對(duì)象來(lái)創(chuàng)建File對(duì)象,然后再調(diào)用這個(gè)File對(duì)象的getName( )就可以了。它會(huì)幫你剝離路徑信息(以一種平臺(tái)無(wú)關(guān)的方式)。然后再在accept( )里面用正則表達(dá)式(regular expression)的matcher對(duì)象判斷,regex是否與文件名相匹配。兜完這個(gè)圈子,list( )方法返回了一個(gè)數(shù)組。 匿名內(nèi)部類(lèi)這是用匿名內(nèi)部類(lèi)來(lái)征程程序的絕佳機(jī)會(huì)。下面我們先創(chuàng)建一個(gè)返回FilenameFileter的filter()方法。/ Uses anonymous inner classe

6、s.import java.io.*;import java.util.*;import com.bruceeckel.util.*;public class DirList2 public static FilenameFilter filter(final String afn) / Creation of anonymous inner class: return new FilenameFilter() String fn = afn; public boolean accept(File dir, String n) / Strip path information: String

7、f = new File(n).getName(); return f.indexOf(fn) != -1; ; / End of anonymous inner class public static void main(String args) File path = new File("."); String list; if(args.length = 0) list = path.list(); else list = path.list(filter(args0); Arrays.sort(list, new AlphabeticComparator(); fo

8、r(int i = 0; i < list.length; i+) System.out.println(listi); 注意,filter( )的參數(shù)必須是final的。要想在匿名內(nèi)部類(lèi)里使用其作用域之外的對(duì)象,只能這么做。這是對(duì)前面所講的代碼的改進(jìn),現(xiàn)在FilenameFilter類(lèi)已經(jīng)與DirList2緊緊地綁在一起了。不過(guò)你還可以更進(jìn)一步,把這個(gè)匿名內(nèi)部類(lèi)定義成list()的參數(shù),這樣代碼會(huì)變得更緊湊:/ Building the anonymous inner class "in-place."import java.io.*;import java.uti

9、l.*;import com.bruceeckel.util.*;public class DirList3 public static void main(final String args) File path = new File("."); String list; if(args.length = 0) list = path.list(); else list = path.list(new FilenameFilter() public boolean accept(File dir, String n) String f = new File(n).getN

10、ame(); return f.indexOf(args0) != -1; ); Arrays.sort(list, new AlphabeticComparator(); for(int i = 0; i < list.length; i+) System.out.println(listi); 現(xiàn)在該輪到main()的參數(shù)成final了,因?yàn)槟涿麅?nèi)部類(lèi)要用它的arg0.這個(gè)例子告訴我們,可以用匿名內(nèi)部類(lèi)來(lái)創(chuàng)建專(zhuān)門(mén)供特定問(wèn)題用的,一次性的類(lèi)。這種做法的好處是,它能把解決某個(gè)問(wèn)題的代碼全部集中到一個(gè)地方。但是從另一角度來(lái)說(shuō),這樣做會(huì)使代碼的可讀性變差,所以要慎重。查看與創(chuàng)建目錄File類(lèi)

11、的功能不僅限于顯示文件或目錄。它還能幫你創(chuàng)建新的目錄甚至是目錄路徑(directorypath),如果目錄不存在的話(huà)。此外它還能用來(lái)檢查文件的屬性(大小,上次修改的日期,讀寫(xiě)權(quán)限等),判斷File對(duì)象表示的是文件還是目錄,以及刪除文件。 renameTo( )這個(gè)方法會(huì)把文件重命名成(或者說(shuō)移動(dòng)到)新的目錄,也就是參數(shù)所給出的目錄。而參數(shù)本身就是一個(gè)File對(duì)象。這個(gè)方法也適用于目錄。輸入與輸出I/O類(lèi)庫(kù)常使用"流(stream)"這種抽象。所謂"流"是一種能生成或接受數(shù)據(jù)的,代表數(shù)據(jù)的源和目標(biāo)的對(duì)象。流把I/O設(shè)備內(nèi)部的具體操作給隱藏起來(lái)了。 正如JD

12、K文檔所示的,Java的I/O類(lèi)庫(kù)分成輸入和輸出兩大部分。所有InputStream和Reader的派生類(lèi)都有一個(gè)基本的,繼承下來(lái)的,能讀取單個(gè)或byte數(shù)組的read( )方法。同理,所有OutputStream和Writer的派生類(lèi)都有一個(gè)基本的,能寫(xiě)入單個(gè)或byte數(shù)組的write( )方法。但通常情況下,你是不會(huì)去用這些方法的;它們是給其它類(lèi)用的 而后者會(huì)提供一些更實(shí)用的接口。因此,你很少會(huì)碰到只用一個(gè)類(lèi)就能創(chuàng)建一個(gè)流的情形,實(shí)際上你得把多個(gè)對(duì)象疊起來(lái),并以此來(lái)獲取所需的功能。Java的流類(lèi)庫(kù)之所以會(huì)那么讓人犯暈,最主要的原因就是"你必須為創(chuàng)建一個(gè)流而動(dòng)用多個(gè)對(duì)象"

13、。 我們最好還是根據(jù)其功能為這些class歸個(gè)類(lèi)。Java 1.0 的類(lèi)庫(kù)設(shè)計(jì)者們是從決定“讓所有與輸入相關(guān)的類(lèi)去繼承InputStream”入手的。同理,所有與輸出相關(guān)的類(lèi)就該繼承OutputStream了。添加屬性與適用的接口使用"分層對(duì)象(layered objects)",為單個(gè)對(duì)象動(dòng)態(tài)地,透明地添加功能的做法,被稱(chēng)為DecoratorPattern。(模式是Thinkingin Patterns (with Java)的主題。)Decorator模式要求所有包覆在原始對(duì)象之外的對(duì)象,都必須具有與之完全相同的接口。這使得decorator的用法變得非常的透明-無(wú)論對(duì)

14、象是否被decorate過(guò),傳給它的消息總是相同的。這也是Java I/O類(lèi)庫(kù)要有"filter(過(guò)濾器)"類(lèi)的原因:抽象的"filter"類(lèi)是所有decorator的基類(lèi)。(decorator必須具有與它要包裝的對(duì)象的全部接口,但是decorator可以擴(kuò)展這個(gè)接口,由此就衍生出了很多"filter"類(lèi))。 Decorator模式常用于如下的情形:如果用繼承來(lái)解決各種需求的話(huà),類(lèi)的數(shù)量會(huì)多到不切實(shí)際的地步。Java的I/O類(lèi)庫(kù)需要提供很多功能的組合,于是decorator模式就有了用武之地。但是decorator有個(gè)缺點(diǎn),在提高編程

15、的靈活性的同時(shí)(因?yàn)槟隳芎苋菀椎鼗旌虾推ヅ鋵傩?,也使代碼變得更復(fù)雜了。Java的I/O類(lèi)庫(kù)之所以會(huì)這么怪,就是因?yàn)樗?quot;必須為一個(gè)I/O對(duì)象創(chuàng)建很多類(lèi)",也就是為一個(gè)"核心"I/O類(lèi)加上很多decorator。 為InputStream和OutputStream定義decorator類(lèi)接口的類(lèi),分別是FilterInputStream和FilterOutputStream。這兩個(gè)名字都起得不怎么樣。FilterInputStream和FilterOutputStream都繼承自I/O類(lèi)庫(kù)的基類(lèi)InputStream和OutputStream,這是deco

16、rator模式的關(guān)鍵(惟有這樣decorator類(lèi)的接口才能與它要服務(wù)的對(duì)象的完全相同)。 用FilterInputStream讀取InputStreamFilterInputStream及其派生類(lèi)有兩項(xiàng)重要任務(wù)。DataInputStream可以讀取各種primitive及String。(所有的方法都以"read"打頭,比如readByte( ), readFloat( )。它,以及它的搭檔DataOutputStream,能讓你通過(guò)流將primitive數(shù)據(jù)從一個(gè)地方導(dǎo)到另一個(gè)地方。這些"地方"都列在表12-4里。 其它的類(lèi)都是用來(lái)修改InputSt

17、ream的內(nèi)部行為的:是不是做緩沖,是不是知道它所讀取的行信息(允許你讀取行號(hào)或設(shè)定行號(hào)),是不是會(huì)彈出單個(gè)字符。后兩個(gè)看上去更像是給編譯器用的(也就是說(shuō),它們大概是為Java編譯器設(shè)計(jì)的),所以通常情況下,你是不大會(huì)用到它們的。 不論你用哪種I/O設(shè)備,輸入的時(shí)候,最好都做緩沖。所以對(duì)I/O類(lèi)庫(kù)來(lái)說(shuō),比較明智的做法還是把不緩沖當(dāng)特例(或者去直接調(diào)用方法),而不是像現(xiàn)在這樣把緩沖當(dāng)作特例。外文原文:JAVA I/O SystemCreating a good input/output (I/O) system is one of the more difficult tasks for the

18、 language designer.This is evidenced by 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), but you need to talk to them in a wi

19、de variety of ways (sequential, 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 d

20、esign actually prevents an explosion 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. As a result there are a fair number of classes to learn before you understa

21、nd 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 bother me with history, just show me how to use it!” The problem is that without the historical perspective you

22、will rapidly become confused with some of the classes and when you should and shouldnt use them. This article will give you an introduction to the variety of I/O classes in the standard Java library and how to use them. The File classBefore getting into the classes that actually read and write data

23、to streams, well look a utility provided with the library to assist you in handling file directory issues. The File class has a deceiving nameyou might think it refers to a file, but it doesnt. It can represent either the name of a particular file or the names of a set of files in a directory. If it

24、s a set of files, you can ask for the set with the list( ) method, and this returns an array of String. It makes sense to return an array rather than one of the flexible container classes because the number of elements is fixed, and if you want a different directory listing you just create a di

25、fferent File object. In fact, “FilePath” would have been a better name for the class. This section shows an example of the use of this class, including the associated FilenameFilter interface. A directory listerSuppose youd like to see a directory listing. The File object can be listed in two ways.

26、If you call list( ) with no arguments, youll get the full list that the File object contains. However, if you want a restricted listfor example, if you want all of the files with an extension of .javathen you use a “directory filter,” which is a class that tells how to select the File objects f

27、or display. The DirFilter class “implements” the interface FilenameFilter. Its useful to see how simple the FilenameFilter interface is: public interface FilenameFilter boolean accept(File dir, String name);The accept( ) method must accept a File object representing the directory that a particu

28、lar file is found in, and a String containing the name of that file. You might choose to use or ignore either of these arguments, but you will probably at least use the file name. Remember that the list( ) method is calling accept( ) for each of the file names in the directory object to se

29、e which one should be includedthis is indicated by the boolean result returned by accept( ). To make sure the element youre working with is only the file name and contains no path information, all you have to do is take the String object and create a File object out of it, then call getName(

30、60;), which strips away all the path information (in a platform-independent way). Then accept( ) uses the a regular expression matcher object to see if the regular expression regex matches the name of the file.Using accept(),the list()method returns an array.Anonymous inner classesThis example

31、is ideal for rewriting using an anonymous inner class. As a first cut, a method filter( ) is created that returns a reference to a FilenameFilter:/ Uses anonymous inner classes.import java.io.*;import java.util.*;import com.bruceeckel.util.*;public class DirList2 public static FilenameFilter fi

32、lter(final String afn) / Creation of anonymous inner class: return new FilenameFilter() String fn = afn; public boolean accept(File dir, String n) / Strip path information: String f = new File(n).getName(); return f.indexOf(fn) != -1; ; / End of anonymous inner class public static void main(String a

33、rgs) File path = new File("."); String list; if(args.length = 0) list = path.list(); else list = path.list(filter(args0); Arrays.sort(list, new AlphabeticComparator(); for(int i = 0; i < list.length; i+) System.out.println(listi); Note that the argument to filter( ) must be final.

34、This is required by the anonymous inner class so that it can use an object from outside its scope. This design is an improvement because the FilenameFilter class is now tightly bound to DirList2. However, you can take this approach one step further and define the anonymous inner class as an argument

35、 to list( ), in which case its even smaller:/ Building the anonymous inner class "in-place."import java.io.*;import java.util.*;import com.bruceeckel.util.*;public class DirList3 public static void main(final String args) File path = new File("."); String list; if(args.lengt

36、h = 0) list = path.list(); else list = path.list(new FilenameFilter() public boolean accept(File dir, String n) String f = new File(n).getName(); return f.indexOf(args0) != -1; ); Arrays.sort(list, new AlphabeticComparator(); for(int i = 0; i < list.length; i+) System.out.println(listi); The argu

37、ment to main( ) is now final, since the anonymous inner class uses args0 directly. This shows you how anonymous inner classes allow the creation of quick-and-dirty classes to solve problems. Since everything in Java revolves around classes, this can be a useful coding technique. One benefit is

38、that it keeps the code that solves a particular problem isolated together in one spot. On the other hand, it is not always as easy to read, so you must use it judiciously. Checking for and creating directoriesThe File class is more than just a representation for an existing file or directory. You ca

39、n also use a File object to create a new directory or an entire directory path if it doesnt exist. You can also look at the characteristics of files (size, last modification date, read/write), see whether a File object represents a file or a directory, and delete a file. The first method thats exerc

40、ised by main( ) is renameTo( ), which allows you to rename (or move) a file to an entirely new path represented by the argument, which is another File object. This also works with directories of any length. Input and outputI/O libraries often use the abstraction of a stream, which represen

41、ts any data source or sink as an object capable of producing or receiving pieces of data. The stream hides the details of what happens to the data inside the actual I/O device. The Java library classes for I/O are divided by input and output, as you can see by looking at the online Java class hierar

42、chy in the JDK documentation. By inheritance, everything derived from the InputStream or Reader classes have basic methods called read( ) for reading a single byte or array of bytes. Likewise, everything derived from OutputStream or Writer classes have basic methods called write( ) for wri

43、ting a single byte or array of bytes. However, you wont generally use these methods; they exist so that other classes can use themthese other classes provide a more useful interface. Thus, youll rarely create your stream object by using a single class, but instead will layer multiple objects togethe

44、r to provide your desired functionality. The fact that you create more than one object to create a single resulting stream is the primary reason that Javas stream library is confusing. Its helpful to categorize the classes by their functionality. In Java 1.0, the library designers started by decidin

45、g that all classes that had anything to do with input would be inherited from InputStream and all classes that were associated with output would be inherited from OutputStream. Adding attributes and useful interfacesThe use of layered objects to dynamically and transparently add responsibilities to

46、individual objects is referred to as the Decorator pattern. The decorator pattern specifies that all objects that wrap around your initial object have the same interface. This makes the basic use of the decorators transparentyou send the same message to an object whether its been decorated or not. T

47、his is the reason for the existence of the “filter” classes in the Java I/O library: the abstract “filter” class is the base class for all the decorators. (A decorator must have the same interface as the object it decorates, but the decorator can also extend the interface, which occurs in several of

48、 the “filter” classes). Decorators are often used when simple subclassing results in a large number of subclasses in order to satisfy every possible combination that is neededso many subclasses that it becomes impractical. The Java I/O library requires many different combinations of features, which

49、is why the decorator pattern is used. There is a drawback to the decorator pattern, however. Decorators give you much more flexibility while youre writing a program (since you can easily mix and match attributes), but they add complexity to your code. The reason that the Java I/O library is awkward to use is that you must create many classesthe “core” I/O type plus all the decoratorsin order to get the single I/O object that you want. The classes that provide the decorator interface to control a particular Input

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論