- 论坛徽章:
- 0
|
两个角度看JAVA的IO流
这个存在心中的顾虑,在某个时候有一点放宽。本来觉得io流已经掌握了,可是当用起来那时,还是显得没有那么得心应手。古语有云:横看成岭侧成峰。一个问题,从多几个角度去看的话,会觉得明白很多。从一个侧面到另外一个侧面去看。
两个角度:一个角度,decorator设计模式;另一个从传统学习的知识完备性出发(API)。
一、Decorator设计模式看IO
UML图很明显了,下面看一个Cat例子:
对应于上图:
Cat-----Component
Java代码- 1.public interface Cat {
- 2. void behavior();
- 3.}
-
复制代码 SmallCat----ConcreteComponent
Java代码- 1.public class SmallCat implements Cat {
- 2. private String name;
- 3.
- 4. SmallCat() {
- 5. }
- 6.
- 7. SmallCat(String name) {
- 8. this.name = name;
- 9. }
- 10.
- 11. public void behavior() {
- 12. // TODO Auto-generated method stub
- 13. System.out.print("I'm small cat! My name is " + name);
- 14. }
- 15.}
复制代码 Decorator----Decorator
Java代码- 1.public interface Decorator extends Cat {
- 2. public void add() ;
- 3.}
-
复制代码 DecoratorDress----ComcreteDecorator1
Java代码- 1.public class DecoratorDress implements Decorator {
- 2. private Cat cat;
- 3.
- 4. public DecoratorDress(Cat cat) {
- 5. this.cat = cat;
- 6. }
- 7.
- 8. public void behavior() {
- 9. cat.behavior();
- 10. add();
- 11. }
- 12.
- 13. public void add() {
- 14. System.out.print(",dressing");
- 15. }
- 16.}
-
复制代码 DecoratorGlasses-----ComcreteDecorator2
Java代码- 1.public class DecoratorGlasses implements Decorator {
- 2. private Cat cat;
- 3.
- 4. DecoratorGlasses(Cat cat) {
- 5. this.cat = cat;
- 6. }
- 7.
- 8. public void add() {
- 9. System.out.print(",glassing");
- 10. }
- 11.
- 12. public void behavior() {
- 13. cat.behavior();
- 14. add();
- 15. }
- 16.}
-
复制代码 main函数:
Java代码- 1.public class TestMain {
- 2. public static void main(String[] args) {
- 3. Cat scat = new SmallCat("xiao hua") ;
- 4. DecoratorDress DressCat = new DecoratorDress(scat);
- 5. DecoratorGlasses GlassesCat = new DecoratorGlasses(dcat) ;
- 6. GlassesCat.behavior();
- 7. }
- 8.}
-
复制代码 这里造了一个猫,然后,让这个小猫穿上衣服,再让它带上眼镜的。呵呵。。。。
言归正传,看看这个io是一个怎样一回事情,他们是这个关系:
这里有四个东西InputStream,OutputStream,Reader,Writer这个接口就构成了JAVA的IO上层。
四个的结构模式都是一样的,用InputStream来举一个例子:
InputStream----Component
FileInputStream同层之类的数据源等-----ComreteComponent
FilterInputStream过滤器----Decorator
DataInputStream同层之类等----ComreteComponent
这样就构成了。
看一例:昨天写的用JAVA实现的简单聊天,用一个client端的代码.
Java代码- 1.import java.io.BufferedReader;
- 2.import java.io.DataInputStream;
- 3.import java.io.DataOutputStream;
- 4.import java.io.InputStreamReader;
- 5.import java.net.Socket;
- 6.public class Client01 {
- 7. public static void main(String[] args) throws Exception {
- 8. DataInputStream dis = null;
- 9. System.out.println("client start..");
- 10. Socket socket = new Socket("127.0.0.1", 8888);
- 11. String str = null;
- 12. while (true) {
- 13. System.out.print("I say:");
- 14. str = new BufferedReader(new InputStreamReader(System.in))
- 15. .readLine();// 读入字符
- 16. if (str.equalsIgnoreCase("88")) {
- 17. break;
- 18. }
- 19. new DataOutputStream(socket.getOutputStream()).writeUTF(str);// 发送数据
- 20. dis = new DataInputStream(socket.getInputStream());// 读入服务器信息
- 21. System.out.println("server say:" + dis.readUTF());// 显示服务器信息
- 22. }
- 23. dis.close();
- 24. socket.close();
- 25. }
- 26.}
复制代码 这里就会看到很多的流的一个接一个情况。呵呵,写的时候头真的有点而昏。这个还得去配合Server端的。
好啦,这个就总结到这里了。
二 、功能分类看IO
这个为了记忆,采用了下面这个方法去记忆了,IO流很多内容,但是想用得顺手还得去记得,还得去敲出来,下面这个从另一个角度看看IO,总结出这个树,没有考究过完整性,常用的估计也包括了。
|--基础流(从特定的数据源读写数据,目的:建立一个数据源)
|--File (文件源)
|--FileReader(FileWriter)
|--FileInputStream(FileInputStream)
|-Memory Array(内存数组源)
|--CharArrayReader(CharArrayWriter)
|--ByteArrayInputStream(ByteArrayOutputStream)
|--Memory String (字符串源)
|--StringReader(StringWriter)
|--(对于字符串,字节没有必要有)
|--Pipe(管道源)
|--PipedReader(PipedWrite)
|--PipedInputStream(PipedOutputStream)
|--高级流(连接在已经存在的流(节点流或处理流)之上,目的:让程序对数据的处理为程序提供更为强大的读写功能)
|--Buffering(把数据源读到绶存,用来对数据的缓冲,目的:提高读写效率)
|--BufferedReader(BufferedWriter)
|--BufferedInputStream(BufferedOutputStream)
|--Coverting (目的:让数据由字节向字符转换)
|--InuputSteamReader(OutputStreamWriter)
|--(没有字符流转成字节流的,这个可以用String中的getByte()函数)
|--Data conversion(对特殊数据字节数处理(数据类型流))
|--(数据是与字符平起平坐的,不用经过字符,再去变成数字)
|--DataInputStream(DataOutputStream)
|--Object Serialization
|--(解释同数据转换)
|--ObjectInputStream(ObjectOutputStream)
|--Printing(实现一个标准的输出,不分数据类型的,没有涉及到输入流的,不会有抛出出的异常)- |--PrintWriter()
-
- |--PrintStream()
-
复制代码 好了,这个是我的一点java学习总结,分享交流一下,还存在不足,请各位指教。
|
|