ImageVerifierCode 换一换
格式:DOCX , 页数:24 ,大小:36.75KB ,
资源ID:9665884      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9665884.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java IO 系统.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java IO 系统.docx

1、Java IO 系统94 翻 译The Java I/O SystemCreating a good input/output (I/O) system is one of the more difficult tasks for the 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 o

2、f 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, random-SQLServer2000, buffered, binary, character, by lines, by words, etc.). The Java library designers attacked this problem by creating lots

3、 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 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 su

4、pplemented 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 improved performance and functionality. As a result, there are a fair number of classes to learn before you understand enough of Javas I/O p

5、icture 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 will rapidly become con

6、fused 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 Java library and how to use them.The File classBefore getting into the classes that actually read and write data to streams, well look at

7、a utility provided with the library to assist you in handling file directory issues.The File class has a deceiving name; you 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 its a set of files, you

8、 can ask for that set using the list() method, which 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 different File object. In fa

9、ct, “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. If you call list() with no

10、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 for display.The DirFilter class “i

11、mplements” the interface FilenameFilter. Its useful to see how simple the FilenameFilter interface is:public interface FilenameFilter boolean accept(File dir, String name);It says all that this type of object does is provide a method called accept(). The whole reason behind the creation of this clas

12、s is to provide the accept() method to the list() method so that list() can “call back” accept() to determine which file names should be included in the list. Thus, this structure is often referred to as a callback. More specifically, this is an example of the Strategy Pattern, because list() implem

13、ents basic functionality, and you provide the Strategy in the form of a FilenameFilter in order to complete the algorithm necessary for list() to provide its service. Because list() takes a FilenameFilter object as its argument, it means that you can pass an object of any class that implements Filen

14、ameFilter to choose (even at run time) how the list() method will behave. The purpose of a callback is to provide flexibility in the behavior of code.DirFilter shows that just because an interface contains only a set of methods, youre not restricted to writing only those methods. (You must at least

15、provide definitions for all the methods in an interface, however.) In this case, the DirFilter constructor is also created.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 u

16、se 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 see which one should be included; this is indicated by the boolean result returned by accept().To make s

17、ure 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(), which strips away all the path information (in a platform-independent way). Then accept() uses a regular ex

18、pression matcher object to see if the regular expression regex matches the name of the file. Using accept(), the list() method returns an array.Input and outputI/O libraries often use the abstraction of a stream, which represents any data source or sink as an object capable of producing or receiving

19、 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 class hierarchy in the JDK documentation. By inheritance, everything derived from the InputStream

20、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 writing a single byte or array of bytes. However, you wont generally use these methods; they exist so

21、 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 together to provide your desired functionality. The fact that you create more than one object to create a

22、 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 deciding that all classes that had anything to do with input would be inherited from InputStream, and all

23、 classes that were associated with output would be inherited from OutputStream.Types of InputStreamInputStreams job is to represent classes that produce input from different sources. These sources can be: 1. An array of bytes. 2. A String object. 3. A file. 4. A “pipe,” which works like a physical p

24、ipe: You put things in at one end and they come out the other. 5. A sequence of other streams, so you can collect them together into a single stream. 6. Other sources, such as an Internet connection. (This is covered in Thinking in Enterprise Java.) Each of these has an associated subclass of InputS

25、tream. In addition, the FilterInputStream is also a type of InputStream, to provide a base class for decorator classes that attach attributes or useful interfaces to input streams. This is discussed later.Types of OutputStreamThis category includes the classes that decide where your output will go:

26、an array of bytes (no String, however; presumably, you can create one using the array of bytes), a file, or a “pipe.” In addition, the FilterOutputStream provides a base class for decorator classes that attach attributes or useful interfaces to output streams.Adding attributes and useful interfacesT

27、he use of layered objects to dynamically and transparently add responsibilities to individual objects is referred to as the Decorator pattern. (Patterns are the subject of Thinking in Patterns (with Java) at www.BruceE.) The decorator pattern specifies that all objects that wrap around your initial

28、object have the same interface. This makes the basic use of the decorators transparentyou send the same message to an object whether it has been decorated or not. This 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 a

29、ll 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 the “filter” classes). Decorators are often used when simple subclassing results in a large number of classes in order to satisfy every

30、 possible combination that is neededso many classes that it becomes impractical. The Java I/O library requires many different combinations of features, and this is the justification for using the decorator pattern.There is a drawback to the decorator pattern, however. Decorators give you much more f

31、lexibility 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

32、that you want.The classes that provide the decorator interface to control a particular InputStream or OutputStream are the FilterInputStream and FilterOutputStream, which dont have very intuitive names. FilterInputStream and FilterOutputStream are derived from the base classes of the I/O library, InputStream and OutputStream, which is the key requirement of the decorator (so that it provides the common interface to all the objects that are being decorated).Reading from an InputStream with FilterInputStreamThe FilterInputStream classes accomplish t

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1