java-IO与装饰器模式

来源:互联网 发布:linux文件强制锁 编辑:程序博客网 时间:2024/06/05 16:35

java-IO

InputStream

read()

读文件,每次读取一个字节,使用FileInputStream,read返回-1表示读到了末尾。InputStream 还可以把数据读取到字节数组内。The DataInputStream allows you to read Java primitives like int, long, float, double, boolean etc. with its corresponding methods readBoolean(), readDouble() etc.

InputStream inputstream = new FileInputStream("c:\\data\\input-text.txt");int data = inputstream.read();while(data != -1) {  //do something with data...  doSomethingWithData(data);  data = inputstream.read();}inputstream.close();

read(byte[])

The InputStream class also contains two read() methods which can read data from the InputStream’s source into a byte array. These methods are:

int read(byte[])int read(byte[], int offset, int length)

Reading an array of bytes at a time is much faster than reading one byte at a time, so when you can, use these read methods instead of the read() method

mark() and reset()

The InputStream class has two methods called mark() and reset() which subclasses of InputStream may or may not support.

If an InputStream subclass supports the mark() and reset() methods, then that subclass should override the markSupported() to return true. If the markSupported() method returns false then mark() and reset() are not supported.

The mark() sets a mark internally in the InputStream which marks the point in the stream to which data has been read so far. The code using the InputStream can then continue reading data from it. If the code using the InputStream wants to go back to the point in the stream where the mark was set, the code calls reset() on the InputStream. The InputStream then “rewinds” and go back to the mark, and start returning (reading) data from that point again. This will of course result in some data being returned more than once from the InputStream.

The methods mark() and reset() methods are typically used when implementing parsers. Sometimes a parser may need to read ahead in the InputStream and if the parser doesn’t find what it expected, it may need to rewind back and try to match the read data against something else.

OutputStream

write(byte)

OutputStream output = new FileOutputStream("c:\\data\\output-text.txt");while(hasMoreData()) {  int data = getMoreData();  output.write(data);}output.close();

write(byte[])

flush

The OutputStream’s flush() method flushes all data written to the OutputStream to the underlying data destination. For instance, if the OutputStream is a FileOutputStream then bytes written to the FileOutputStream may not have been fully written to disk yet. The data might be buffered in memory somewhere, even if your Java code has written it to the FileOutputStream. By calling flush() you can assure that any buffered data will be flushed (written) to disk (or network, or whatever else the destination of your OutputStream has).

BufferedReader

BufferReader的作用是为其它Reader提供缓冲功能。创建BufferReader时,我们会通过它的构造函数指定某个Reader为参数。BufferReader会将该Reader中的数据分批读取,每次读取一部分到缓冲中;操作完缓冲中的这部分数据之后,再从Reader中读取下一部分的数据。
为什么需要缓冲呢?原因很简单,效率问题!缓冲中的数据实际上是保存在内存中,而原始数据可能是保存在硬盘或NandFlash中;而我们知道,从内存中读取数据的速度比从硬盘读取数据的速度至少快10倍以上。
那干嘛不干脆一次性将全部数据都读取到缓冲中呢?第一,读取全部的数据所需要的时间可能会很长。第二,内存价格很贵,容量不想硬盘那么大。

这么看来BufferedReader一般用来读取大文件,并且部分数据是有用的情况,比如socket传输数据

BufferedInputStream

BufferedInputStream和普通InputStream比,其实就是多了个缓冲区,如下代码所示,BufferedInputStream用read来读取的也是一个字节,只不过这个字节,一般是来自缓冲区的。但是FileInputStream来read的话,那这个字节是来自文件的,明显来自缓冲区要快很多,缓冲区在内存里面,

 String file = "out.txt";      InputStream ins = new FileInputStream(file);  BufferedInputStream bufin= new BufferedInputStream(ins);  int b;  while((b=bufin.read())!=-1){   System.out.println(Integer.toHexString(b));  }

装饰器模式

FilterInputStream是正宗的装饰器模式的父类,他继承了InputStream,并且内部持有了InputStream实例in,在各种方法内available,close,write,read里都是调用in的相关方法。所以我们在子类里面可以对相关方法重写来达到装饰的目的。FilterInputStream的子类有DataInputStream、BufferedInputStream,他们都是经典装饰器模式,对父类的各个方法进行了重写。
http://www.cnblogs.com/zuoxiaolong/p/pattern11.html 这个文章的例子写的不错。

特点:
装饰者类拥有被装饰者类的对象,一般是当构造参数传入。
在装饰者类当中调用被装饰者类的方法,封装成新的功能方法。

thanks to

http://tutorials.jenkov.com/java-io/inputstream.html
http://www.cnblogs.com/skywang12345/p/io_23.html