1. streams及i/o
stream就是信息源与目的地之间的通信路径,这里的信息源可以是文件、内存、网络等。streams主要分为input及output stream。
1.1 inputstream类
类inputstream处于input stream类层次的最顶层,它主要具有以下几种方法:
1.1.1 read方法
read方法用于从指定的输入流读取以字节为单位的数据,第一次从流的开始位置开始读取,以后每次从上次的结束部位开始读取,即自动实现了位移。
read方法有以下三种形态:
(1) int read(byte buff[n]):从指定输入流中读取n个字节填充到buff中,该方法返回读取的实际字节数,如果读取的实际字节数小于n,一般是因为已读到指定输入流的末尾;
(2) int read():即不带参数,该方法每次一个字节从指定的输入流中读取数据。返回值也是int类型,但它并不代表读取的字节数,而是从流中读取的数据的本身,因数据本身是byte类型的,所以一般要强制进行转化;如果读到流的末尾返回的值是-1;
(3) int read(byte buff[n],int start, int len):从指定流读取数据,从start开始,填充len个字节到buff中,返回值为实际的填充数,如果返回值<len,一般表示已将指定流中的数据读完;
以下是read的简单例子:
import java.io.*;
class testio1{
public static void main(string args[]) {
inputstream s = null;
try{
s = new fileinputstream("io.txt");
}catch(filenotfoundexception e){
system.out.println("file not find");
}
int i;
try{
i = s.read();
while(i != -1){
system.out.println((char)i);
i = s.read();
}
}catch(ioexception e){
system.out.println("io error");
} } }
1.1.2 skip方法
skip方法类似于c语言中的lseek都是用于定位的。skip方法定义:long skip(long n),该方法使指定流中的当前位置移动n个字节,n的值可以是负值用于向前移,skip方法返回值为实际移动的字节数,由于种种原因,如已到流尾或者其它原因返回的值往往小于n。对于读取文件来说,小于n的原因最大的原因是读到了文件尾。
1.1.3 available方法
available方法用于计算指定流中当前有多少字节,如果指定的流是文件流,那么就返回文件的大小。available返回的值是int类型。
有的输入流可能没有能力返回字节数,如果对这些输入流使用avaiable方法,返回值为0。
1.1.4 close方法
对于打开的stream,java可以自动回收,但是java自动回收需要时间,所以最好自己调用close方法来关闭stream,这样方便下一次重新指定的流。
1.2 bytearrayinputstream
bytearrayinputstream是从inputstream中继承下来的,用于从字节数组中提取数据,关于bytearrayinputstream的创建例子如下:
byte[] buffer = new byte[1024];
fillwithusefuldata(buffer); //自定义的方法,用于在buffer中填充数据
inputstream s = new bytearrayinputstream(buffer);
inputstream s1 = new bytearrayinputstream(buffer,100,300);
其中bytearrayinputstream(buffer,100,300)是创建到buffer的stream,从buffer的第100个字节开始取300字节。
bytearrayinputstream的其它方法与inputstream类似,这里不再重复。
1.3 fileinputstream
fileinputstream也是从inputstream中继承下来的,用于从指定的文件中提取。因此它的方法也与inputstream中的方法类似,这里不再介绍,只介绍fileinputstream中特殊的方法:getfd(),该方法用于获取文件句柄。使用方法如下:
fileinputstream afis = new fileinputstream("afilename");
filedescriptor myfd = afis.getfd();
这样以后要用到afilename文件时可以使用myfd这个文件句柄(实际上是文件描述类的实例),如要重新打开该文件,可以使用fileinputstream afis = new fileinputstream(myfd)。
关于文件描述类filedescriptor,有以下几点说明:
(1) 属性in:标准输入;
(2) 属性out:标准输出;
(3) 属性err:标准错误输出;
在fileinputstream中还有另一个特殊的方法就是:finalize()。
1.4 filterinputstream
filterinputstream也是从inputstream中继承下来,不过filterinputstream类基本上不能直接使用,一般上使用该类的派生类,如bufferedinputstream等。该类的最大特点是,在定义时可以嵌套:
inputstream s = getaninputstreamfromsomewhere();
filterinputstream s1 = new filterinputstream(s);
filterinputstream s2 = new filterinputstream(s1);
filterinputstream s3 = new filterinputstream(s2);
所以该类的所有派生类都具有这个特性。
1.5 bufferedinputstream
bufferedinputstream指定数据源是内存的指定区域,从filterinputstream继承下来的,这种类型的stream主要用于提高性能,它定义时一般指定其它的inputstream,如:
inputstream s = new bufferedinputstream(new fileinputstream("foo"));
bufferedinputsream是可以使用mark及reset方法,使用上述的嵌套方法间接的使其它的stream也支持这些方法了。
由于bufferedinputstream需要buffer,所以它需要等待在它之前的数据都到了后才工作,所以bufferedinputstream最好用在流的前面,如上面这个例子,当然用在最前面也没有什么意思了。
1.6 datainputstream
datainputstream也是从filterinputstream继承下来的,所以也具有父类的特性。datainputstream还implement datainput接口,所以datainputstream具体的方法最多,如:readshort、readboolean、readbyte、readunsignedbyte、readshort等。这些方法的都是read方法的扩展,使用上也相似,所以这里不多介绍。
以下是readint的实现:
public final int readint() throws ioexception {
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new eofexception();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}
以下是readline的实现:
public final string readline() throws ioexception {
char buf[] = linebuffer;
if (buf == null) {
buf = linebuffer = new char[128];
}
int room = buf.length;
int offset = 0;
int c;
loop: while (true) {
switch (c = in.read()) {
case -1:
case '/n':
break loop;
case '/r':
int c2 = in.read();
if ((c2 != '/n') && (c2 != -1)) {
if (!(in instanceof pushbackinputstream)) {
this.in = new pushbackinputstream(in);
}
((pushbackinputstream)in).unread(c2);
}
break loop;
default:
if (--room < 0) {
buf = new char[offset + 128];
room = buf.length - offset - 1;
system.arraycopy(linebuffer, 0, buf, 0, offset);
linebuffer = buf;
}
buf[offset++] = (char) c;
break;
}
}
if ((c == -1) && (offset == 0)) {
return null;
}
return string.copyvalueof(buf, 0, offset);
}
在这个例子中,如果读出的字符中/r,还得再读一位判断是否是/n,如果不是/n,还得将这个字符放回到输入流中,因此使用了pushbackinputstream的功能。
如果对datainputstream的读操作已到stream的末尾,会抛出eofexception异常。在stream末尾,skipbytes不做任何动作;readline返回null;readutf抛出utfdataformatexception异常。
1.7 linenumberinputstream
同样linenumberinputstream是从filterinputstream继承下来的,该
闽公网安备 35060202000074号