IO(2)

继续前面一节的IO流,这里先介绍一个PushbackInputStream类,这个类也是刚刚学习的。
先看一下官方文档,这个类基本的功能就是能读取一个一个字节,然后判断是否是我们想要的,如果不想要的话可以重新把它送回缓冲区。
public int read()throws IOException
从此输入流中读取下一个数据字节。以 int 形式返回 0 到 255 范围内字节值。如果因为已经到达流末尾而没有可用的字节,则返回值 -1。在输入数据可用、检测到流末尾或者抛出异常之前,此方法一直阻塞。此方法返回最近一次推回的字节(如果有),否则将调用其底层输入流的 read 方法,并返回该方法返回的任何值。
public void unread(int b)throws IOException
推回一个字节:将其复制到推回缓冲区之前。此方法返回后,要读取的下一个字节的值将为 (byte)b。  
例子:
PushbackInputStream pbin = new PushbackInputStream(new BufferedInputStream(new FileInputStream("employee.dat")));
然后可以读它的下一个字节:
int b = pbin.read();
如果当前的字节不是我们要的,可以重新把它拷贝一份送回到缓冲区
if(b != '<') pbin.unread(b);
这时你调用pbin.read();读取的字符就是上次你回退的那个字符,如果你想继续读取下一个字符,就需要调用pbin.read()

在书上有这样的一个例子,它的解释是这样的,但是我不知它这样的做法有什么好处,如果有人知道的,请跟我解释一下:
But reading and unreading are the only methods that apply to the pushback inputstream. If you want to look ahead and also read numbers, then you need both a pushback input stream and a data input stream reference.
DataInputStream din = new DataInputStream(pbin = new PushbackInputStream(new BufferedInputStream(new FileInputStream("employee.dat"))));
它的说法是读取和推回方法只是适用于pushbackInputStream,如果你想要向前看并且读取数据类型出来就需要拥有DataInputStream和PushbackInputStream这两个类的实例,我有点纳闷,DataInputStream类中都没有类似unread()的方法,那么能向前预读一个数据类型又有什么用,读完后又不能把它送回缓冲区中,请问有人能指点一下!^_^
难道它的意思是想要重写一个类去继承DataInputStream类,在类中拥有PushbackInputStream的实例,然后自己去实现unread方法???
如果要你从压缩数据流中读取出具体的数据类型出来你会怎么做,其实这一个在上一讲说过了,这个模式很重要,类似代理,这里附上图片一张,其实就是调用DataInputStream的read方法追踪到最后会由它的参数FileInputStream的read方向去实现:这个就是儿子不做让它的老爸去做.
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream("you path"));
DataInputStream dataInputStream = new DataInputStream(zipInputStream);


下面总结一下这几个类:
java.io.FileInputStream
FileInputStream(String name)
FileInputStream(File file)
creates a new file input stream, using the file whose path name is specified by the name string or the file object. (The File class is described at the end of this chapter.) Path names that are not absolute are resolved relative to the working directory that was
set when the VM started.

java.io.FileOutputStream
FileOutputStream(String name)
FileOutputStream(String name, boolean append)
FileOutputStream(File file)
FileOutputStream(File file, boolean append)
creates a new file output stream specified by the name string or the file object. (The File class is described at the end of this chapter.) If the append parameter is true, then data are added at the end of the file. An existing file with the same name will not
be deleted. Otherwise, this method deletes any existing file with the same name.
其中参数append表示每次写向文件写数据时都是从末尾处写。
java.io.BufferedInputStream 1.0
BufferedInputStream(InputStream in)
creates a buffered stream. A buffered input stream reads characters from a stream without causing a device access every time. When the buffer is empty, a new block of data is read into the buffer.
表示每次都把数据先存到缓冲区中来,然后一次性把数据读到程序中来,这样就避免了每次重复的读取硬盘
java.io.BufferedOutputStream 1.0
BufferedOutputStream(OutputStream out)
creates a buffered stream. A buffered output stream collects characters to be written without causing adevice access every time. When the buffer fills up or when the stream is flushed, the data are written.




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值