(完整版)Java外文翻译1毕业设计论文

上传人:MM****y 文档编号:73776567 上传时间:2022-04-12 格式:DOC 页数:17 大小:207.50KB
收藏 版权申诉 举报 下载
(完整版)Java外文翻译1毕业设计论文_第1页
第1页 / 共17页
(完整版)Java外文翻译1毕业设计论文_第2页
第2页 / 共17页
(完整版)Java外文翻译1毕业设计论文_第3页
第3页 / 共17页
资源描述:

《(完整版)Java外文翻译1毕业设计论文》由会员分享,可在线阅读,更多相关《(完整版)Java外文翻译1毕业设计论文(17页珍藏版)》请在装配图网上搜索。

1、优秀论文未经允许审核通过切勿外传毕业设计(论文)外文文献翻译译文:Java IO系统 1对编程语言的设计者来说,创建一套好的输入输出(IO)系统,是一项难度极高的任务。这一类可以从解决方案的数量之多上看出端倪。这个问题就难在它要面对的可能性太多了。不仅是因为有那么多的IO 的源和目的(文件,控制台,网络连接等等),而且还有很多方法(顺序的,随机的,缓存的,二进制的,字符方式的,行的,字的等等)。Java 类库的设计者们用“创建很多类”的办法来解决这个问题。坦率地说, Java IO 系统的类实在太多了,以至于初看起来会把人吓着(但是,具有讽刺意味的是,这种设计实际上是限制了类的爆炸性增长)。此

2、外,Java 在 1.0 版之后又对其 IO 类库进行了重大的修改, 原先是面向 byte 的,现在又补充了面向Unicode 字符的类库。为了提高性能,完善功能,JDK1.4又加了一个 nio( 意思是“ new IO”。这个名字会用上很多年 ) 。这么以来,如果你想对 Java 的 IO 类库有个全面了解, 并且做到运用自如,你就得先学习大量的类。此外,了解 IO 类库的演化历史也是相当重要的。可能你的第一反应是 “别拿什么历史来烦我了, 告诉我怎么用就可以了! ”但问题是,如果你对这段一无所知,很快就会被一些有用或是没用的类给搞糊涂了。本文会介绍 Java 标准类库中的各种IO 类,及其

3、使用方法。File类在介绍直接从流里读写数据的类之前,我们先介绍一下处理文件和目录的类。你会认为这是一个关于文件的类,但它不是。你可以用它来表示某个文件的名字,也可以用它来表示目录里一组文件的名字。如果它表示的是一组文件,那么你还可以用 list( ) 方法来进行查询,让它会返回 String 数组。由于元素数量是固定的,因此数组会比容器更好一些。如果你想要获取另一个目录的清单,再建一个 File 对象就是了。目录列表器假设你想看看这个目录。有两个办法。一是不带参数调用 list( ) 。它返回的是 File 对象所含内容的完整清单。但是,如果你要的是一个 限制性列表 (restricted

4、list)的话比方说,你想看看所有扩展名为 .java 的文件 那么你就得使用 目录过滤器 了。这是一个专门负责挑选显示 File 对象的内容的类。FilenameFilter接口的声明:public interface FilenameFilter boolean accept(File dir, String name);accept( )方法需要两个参数,一个是File对象,表示这个文件是在哪个目录里面的;另一个是String ,表示文件名。虽然你可以忽略它们中的一个,甚至两个都不管, 但是你大概总得用一下文件名吧。记住,list()会对目录里的每个文件调用accept( ),并以此判断

5、是不是把它包括到返回值里;这个判断依据就是accept( )的返回值。切记,文件名里不能有路径信息。为此你只要用一个String对象来创建 File对象,然后再调用这个File对象的 getName( ) 就可以了。它会帮你剥离路径信息(以一种平台无关的方式)。然后再在 accept() 里面用正则表达式 (regular expression)的 matcher 对象判断, regex 是否与文件名相匹配。兜完这个圈子,list( )方法返回了一个数组。匿名内部类这是用匿名内部类来征程程序的绝佳机会。下面我们先创建一个返回FilenameFileter的 filter()方法。Uses an

6、onymous inner classes.importimportimportpublicclass DirList2 publicstaticFilenameFilterfilter(finalString afn) Creation of anonymous inner class:returnnew FilenameFilter() publicboolean accept(File dir, String n) Strip path information:String f =new File(n).getName();returnf.indexOf(fn) != -1;End of

7、 anonymous inner classpublic static File path = String list;void main(String args) new File(. );if (args.length = 0)list = path.list();elselist = path.list(filter(args0);Arrays.sort(list,new AlphabeticComparator();for ( inti = 0; i list.length; i+)注意, filter( )的参数必须是final的。要想在匿名内部类里使用其作用域之外的对象,只能这么做

8、。这是对前面所讲的代码的改进,现在FilenameFilter类已经与DirList2紧紧地绑在一起了。不过你还可以更进一步,把这个匿名内部类定义成 list()的参数,这样代码会变得更紧凑:Building the anonymous inner class in-place.importimportimportpublicclass DirList3 publicstaticvoid main( finalString args) File path =new File(. );String list;if (args.length = 0)list = path.list();elsel

9、ist = path.list(new FilenameFilter() publicbooleanaccept(File dir, String n) String f =newFile(n).getName();returnf.indexOf(args0) != -1;);Arrays.sort(list,new AlphabeticComparator();for ( inti = 0; i list.length; i+)现在该轮到main() 的参数成final了,因为匿名内部类要用它的arg0.这个例子告诉我们,可以用匿名内部类来创建专门供特定问题用的,一次性的类。这种做法的好处是

10、,它能把解决某个问题的代码全部集中到一个地方。但是从另一角度来说,这样做会使代码的可读性变差,所以要慎重。查看与创建目录File类的功能不仅限于显示文件或目录。它还能帮你创建新的目录甚至是目录路径 (directorypath),如果目录不存在的话。此外它还能用来检查文件的属性 ( 大小,上次修改的日期,读写权限等) ,判断File对象表示的是文件还是目录,以及删除文件。renameTo( ) 这个方法会把文件重命名成( 或者说移动到 ) 新的目录,也就是参数所给出的目录。而参数本身就是一个File对象。这个方法也适用于目录。输入与输出IO 类库常使用 流 (stream) 这种抽象。所谓 流

11、 是一种能生成或接受数据的,代表数据的源和目标的对象。流把IO 设备内部的具体操作给隐藏起来了。正如 JDK 文档所示的, Java 的 IO 类库分成输入和输出两大部分。所有 InputStream 和 Reader 的派生类都有一个基本的,继承下来的,能读取单个或 byte 数组的 read( ) 方法。同理,所有 OutputStream 和 Writer的派生类都有一个基本的,能写入单个或byte 数组的 write( )方法。但通常情况下,你是不会去用这些方法的;它们是给其它类用的 而后者会提供一些更实用的接口。因此,你很少会碰到只用一个类就能创建一个流的情形,实际上你得把多个对象叠

12、起来,并以此来获取所需的功能。Java 的流类库之所以会那么让人犯晕,最主要的原因就是 你必须为创建一个流而动用多个对象 。我们最好还是根据其功能为这些class归个类。Java 1.0的类库设计者们是从决定“让所有与输入相关的类去继承InputStream”入手的。同理,所有与输出相关的类就该继承OutputStream了。添加属性与适用的接口使用 分层对象(layered objects),为单个对象动态地,透明地添加功能的做法, 被称为 DecoratorPattern。( 模式是 ThinkinginPatterns(with Java)的主题。 )Decorator模式要求所有包覆在

13、原始对象之外的对象,都必须具有与之完全相同的接口。这使得decorator的用法变得非常的透明 - 无论对象是否被decorate过,传给它的消息总是相同的。这也是 Java IO 类库要有 filter( 过滤器 ) 类的原因:抽象的 filter 类是所有 decorator 的基类。(decorator 必须具有与它要包装的对象的全部接口,但是 decorator 可以扩展这个接口,由此就衍生出了很多 filter 类) 。Decorator 模式常用于如下的情形: 如果用继承来解决各种需求的话,类的数量会多到不切实际的地步。Java 的 IO 类库需要提供很多功能的组合,于是 deco

14、rator 模式就有了用武之地。但是 decorator 有个缺点,在提高编程的灵活性的同时 ( 因为你能很容易地混合和匹配属性 ) ,也使代码变得更复杂了。 Java 的 IO 类库之所以会这么怪, 就是因为它 必须为一个 IO 对象创建很多类 ,也就是为一个 核心 IO 类加上很多 decorator 。为 InputStream 和 OutputStream 定义 decorator 类接口的类,分别是 FilterInputStream和 FilterOutputStream。这两个名字都起得不怎么样。 FilterInputStream和 FilterOutputStream都继承自

15、 IO 类库的基类InputStream 和 OutputStream ,这是 decorator模式的关键 ( 惟有这样decorator 类的接口才能与它要服务的对象的完全相同) 。用 FilterInputStream读取 InputStreamFilterInputStream及其派生类有两项重要任务。DataInputStream可以读取各种 primitive及 String 。( 所有的方法都以 read 打头,比如readByte( ) , readFloat( )。它,以及它的搭档 DataOutputStream ,能让你通过流将 primitive数据从一个地方导到另一个

16、地方。这些 地方 都列在表 12-4 里。其它的类都是用来修改 InputStream 的内部行为的:是不是做缓冲,是不是知道它所读取的行信息 ( 允许你读取行号或设定行号 ) ,是不是会弹出单个字符。后两个看上去更像是给编译器用的( 也就是说,它们大概是为 Java 编译器设计的 ) ,所以通常情况下,你是不大会用到它们的。不论你用哪种IO 设备,输入的时候,最好都做缓冲。所以对IO 类库来说,比较明智的做法还是把不缓冲当特例( 或者去直接调用方法 ) ,而不是像现在这样把缓冲当作特例。外文原文:JAVA IO SystemCreating a good inputoutput (IO) s

17、ystem is one of themore difficult tasks for the language designer.This is evidenced by the number of different approaches. The challengeseems to be in covering all eventualities. Not only are there different sources and sinks of IO that you want to communicate with (files, theconsole, network connec

18、tions),but you need to talk to them in a wide variety of ways (sequential, random-access, buffered, binary, character, by lines, by words, etc.).The Java library designers attacked this problem by creating lots ofclasses. In fact, there are so many classes for Java s IO system that it cbe intimidati

19、ng at first (ironically, the Java IO design actually prevents an explosion of classes). There was also a significant change in the IO library after Java 1.0, when the original byte-oriented library was supplemented with char-oriented, Unicode-based IO classes. As a result there are a fair number of

20、classesto learn before you understand enough of Java sIOpicture that you can use it properly. In addition, it s rather importantunderstand the evolutionif your first reaction is“ don t bother me withyou should and shouldn t use them.This article will give you an introduction to the variety of IO cla

21、sses inthe standard Java library and tIt. can represent either the name of aparticular file or the names of a set of files in a directory. If ityou can ask for the set with the list( ) method, and this returns an array ofString. It makes senseto return an array rather than one of the flexiblecontain

22、er classes because the number of elements is fixed, and if you wanta different directory listing you just create a different File object. In fact,“ FilePath would”a better name for the class. This section shows anexample of the use of this class, including the associatedFilenameFilter interface.A di

23、rectory listerSuppose you d like to see a directory listing. The File object can be listed in two ways. If you call list() with no arguments, you ll 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 .

24、java then you use a“ directory filter, ” whichsisthatclastells accept(File dir, String name);The accept( ) method must accept a File object representing the directory that a particular file is found in, and a String containing the name of that file. You might choose to use or ignore either of these

25、arguments, but you will probably at least use the file name. Remember thatthe list( ) method is calling accept( ) for each of the file names in the directory object to see which one should be includedthis is indicated by the boolean result returned by accept( ).To make sure the element you re workin

26、g with is only the file name andcontains no path information, all you call getName( ), 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 acce

27、pt(),the list()method returns an array.Anonymous inner classesThis example 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.importimportimportpublicclass DirList2 publicstati

28、cFilenameFilterfilter(finalString afn) Creation of anonymous inner class:returnnew FilenameFilter() String fn = afn;publicboolean accept(File dir, String n) Strip path information:String f =new File(n).getName();returnf.indexOf(fn) != -1;End of anonymous inner classpublic static File path = String l

29、ist;void main(String args) new File(. );if (args.length = 0)list = path.list();elselist = path.list(filter(args0);Arrays.sort(list,new AlphabeticComparator();for ( inti = 0; i list.length; i+)Note that the argument to filter( ) must be final. This is required by the anonymous inner class so that it

30、can use an object from outside its scope. This design is an improvement because the FilenameFilter class isnow tightly bound to DirList2. However, you can take this approach one step further and define the anonymous inner class as an argument to list( ), inwhich case it s evenller:smaBuilding the an

31、onymous inner class in-place.importimportimportpublicclass DirList3 publicstaticvoid main( finalString args) File path =new File(. );String list;if (args.length = 0)list = path.list();elselist = path.list(new FilenameFilter() publicbooleanaccept(File dir, String n) String f =newFile(n).getName();ret

32、urnf.indexOf(args0) != -1;);Arrays.sort(list,new AlphabeticComparator();for ( inti = 0; i list.length; i+)The argument to main( ) is now final, since the anonymous inner classuses args0 directly.This shows you of quick-and-dirty classesto solve problems. Since everything in Java revolves around clas

33、ses,this can be a useful coding technique. One benefit is that it keeps the code that solves a particular problem isolated together in one spot. On the other just a representation for an existing file or directory. You can also use a File object to create anew directory or an entire directory path i

34、f it doesn t exist. You can al look at the characteristics of files (size, last modification date, readwrite), see whether a File object represents a file or a directory, and delete a file. The first method that s exercised by) main(isrenameTo( ), which allowsyou to rename (or move) a file to an ent

35、irely new path represented by the argument, which is another File object. This also works with directories of any length.Input and outputIO libraries often use the abstraction of a stream, which represents any data source or sink as an object capable of producing or receiving pieces of data. The str

36、eam see by looking at the online Java class the JDK documentation. By inheritance, everything derived from the InputStreamor Reader classes generallyt use these methods; they exist so that otherclasses can use them these other classes provide a more useful interface. Thus, you llrarely create your s

37、tream object by using a single class, but instead will layer multiple objects together to provide your desiredfunctionality. The fact that you create more than one object to create asingle resulting stream si the primary reason that Java s stream library isconfusing.It sJava 1.0, the library designe

38、rs started by deciding that all classesthat . The decorator pattern specifiesthat all objects that wrap aroundyour initial objectobject whether it s decoratedben or not. This is thereason for the existence of the“ filter in”theclassesJavaIOlibrary: theabstract “ filter ” class is the base class for

39、all the decorators. (A decorator must also extend the interface, which occurs in several of the “ filter ” classes).Decorators are often used when simple subclassingresults in a large number of subclasses in order to satisfy every possible combination that is neededso many subclassesthat it becomes

40、impractical. The Java IO library requires many different combinations of features, which is why the decorator pattern is used. There is a drawback to the decorator pattern, easily mix and match attributes), but they add complexity to your code. The reason that the Java IO library is awkward to use i

41、s that you must create many classes the “ core ” IO type plus all the decoratorsin order to get the single IO object that you want.The classesthat provide the decorator interface to control a particular InputStream or OutputStream are the FilterInputStream and FilterOutputStream which don t interfac

42、e to all the objects that are being decorated).Reading from an InputStream withFilterInputStreamThe FilterInputStream classes accomplish two significantly differentthings. DataInputStream allows you to read different types of primitivedata as well as String objects. (All the methods start with“ read

43、,”sreadByte( ),readFloat( ),etc.)This,alongwithitscompanionDataOutputStream, allows you to move primitive data from one place to another via a stream. These “ places ” are determined by the classes in Table 12-4Table 12-4. Types of FilterInputStreamTheremaining classes modify the wayan InputStreambe

44、havesinternally: whether it s buffered or unbuffered, if it keeps track of the linesit s reading (allowing you to ask for line numbers or set the line number), and whether you can push back a single character. The last two classes look a lot like support for building a compiler (that is, they were added tosupport the construction of theJava compiler), so you probably won t usethem in general programming.You ll probably need to buffer your input almost every time, regardless of the IO device you re connecting to, so it wouldbuffered input.

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!