-
1.
Streams
及
I/O
Stream
就 是信息源与目的地之间的通信路径,
这里的信息源可以是文件、
内存、
网络等。
Streams
主要分为
input
及
output Stream
。
1.1 InputStream
类
类
InputStream
处于
Input stream
类层次的最顶层,它主要具有以下几种方法:
1.1.1 read
方法
read
方法用于从指定的输入流读取以字节为单位的数据,
第一次从流的开始位置开始读
取,以后每次从上次的结束部位开始读取,即自动实现了位移。< br>
read
方法有以下三种形态:
(
1
)
int read(byte buff[n])
:从指定输入流中读取
n
个字节填充到
buff
中,该方法返回
读取 的实际字节数,如果读取的实际字节数小于
n
,一般是因为已读到指定输入流的末
尾;
(
2
)
int
read():即不带参数,该方法每次一个字节从指定的输入流中读取数据。返回
值也是
int
类型,但它并不代表读取的字节数,而是从流中读取的数据的本身,因数据
本身是
byte< br>类型的,所以一般要强制进行转化;如果读到流的末尾返回的值是
-1
;
(
3
)
int read(byte buff[n],int start, int len)
:从指定流读取数据,从
star t
开始,填充
len
个字节到
buff
中,返回值为实际的填充数, 如果返回值
数据读完;
以下是
read
的简单例子:
import .*;
class TestIO1{
public static void main(String args[]) {
InputStream s = null;
try{
s = new FileInputStream(
}catch(FileNotFoundException e){
n(
}
int i;
try{
i = ();
while(i != -1){
n((char)i);
i = ();
}
}catch(IOException e){
n(
} } }
1.1.2 skip
方法
skip
方法类似于
C
语言中的
lseek
都是用于定位 的。
Skip
方法定义:
long skip(long n)
,
该 方法使指定流中的当前位置移动
n
个字节,
n
的值可以是负值用于向前移,< br>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(
FileDescriptor myFD = ();
这样以后要用到
aFileName
文件时可以使用
m yFD
这个文件句柄(实际上是文件描述类
的
实
例
)
,如
要
重
新
打
开
该
文
件
,可
以
使
用
FileInputStream
aFIS
=
new
FileInputStream(myFD)
。
关于文件描述类
FileDescriptor
,有以下几点说明:
(
1
)
属性
in
:标准输入;
(
2
)
属性
out
:标准输出;
(
3
)
属性
err
:标准错误输出;
在
FileIn putStream
中还有另一个特殊的方法就是:
finalize()
。
1.4 FilterInputStream
FilterInputStre am
也是从
InputStream
中继承下来,不过
FilterInpu tStream
类基本上不能
直接使用,一般上使用该类的派生类,如
Buffere dInputStream
等。该类的最大特点是,
在定义时可以嵌套:
InputStream s = getAnInputStreamFromSomewhere();
FilterInputStream s1 = new FilterInputStream(s);
FilterInputStream s2 = new FilterInputStream(s1);
FilterInputStream s3 = new FilterInputStream(s2);
所以该类的所有派生类都具有这个特性。
1.5 BufferedInputStream
BufferedInputStream
指定数 据源是内存的指定区域,
从
FilterInputStream
继承下来的,
这
种类型的
Stream
主要用于提高性能,它定义时一般指定其它的
In putStream
,如:
InputStream s = new BufferedInputStream(new FileInputStream(
B ufferedInputSream
是可以使用
mark
及
reset方法,使用上述的嵌套方法间接的使其它
的
stream
也支持这些方法了。
由于
BufferedInputStream
需要
buf fer
,
所以它需要等待在它之前的数据都到了后才工作,
所以
Buffer edInputStream
最好用在流的前面,如上面这个例子,当然用在最前面也没有
什么 意思了。
1.6 DataInputStream
DataInputStream
也是从
FilterInputStream继承下来的,所以也具有父类的特性。
DataInputStream
还
imp lement
DataInput
接口,所以
DataInputStream具体的方法最多,
如:
readShort
、
readBoolean< br>、
readByte
、
readUnsignedByte
、
readShort
等。这些方法的
都是
read
方法的扩展,使 用上也相似,所以这里不多介绍。
以下是
readInt
的实现:
public final int readInt() throws IOException {
int ch1 = ();
int ch2 = ();
int ch3 = ();
int ch4 = ();
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 =
int offset = 0;
int c;
loop: while (true) {
switch (c = ()) {
case -1:
case 'n':
break loop;
case 'r':
int c2 = ();
if ((c2 != 'n') && (c2 != -1)) {
if (!(in instanceof PushbackInputStream)) {
= new PushbackInputStream(in);
}
((PushbackInputStream)in).unread(c2);
}
break loop;
default:
if (--room < 0) {
buf = new char[offset + 128];
room = - offset - 1;
opy(lineBuffer, 0, buf, 0, offset);
lineBuffer = buf;
}
buf[offset++] = (char) c;
break;
}
}
if ((c == -1) && (offset == 0)) {
return null;
}
return lueOf(buf, 0, offset);
}
在这个例子中,如果读出的字符中
r
,还得再读一位判断是否是
n
,如果不是
n
,还得将
这个字符放回到输入流中,因此使用了
Pus hbackInputStream
的功能。
如果对
DataI nputStream
的读操作已到
Stream
的末尾,会抛出
EOFEx ception
异常。在
Stream
末
尾
,
skipBy tes
不
做
任
何
动
作
;
readLine
返
回
null
;
readUTF
抛
出
UT FDataFormatException
异常。
1.7 LineNumberInputStream
同样
LineNumberInputStr eam
是从
FilterInputStream
继承下来的,
该类可以跟踪 行号,
设
置行号,
对行作标记以便恢复等功能。
一般不直接使用该类,
而使用其它
Stream
的嵌套
间接对它进行使用,如:
LineNumberInputStream aLNIS;
aLNIS = new LineNumberInputStream(new FileInputStream(
DataInputStream s = new DataInputStream(aLNIS);
String line;
while ((line = ne()) != null) {
. . . // process the line
n(
}
在这个例子中,使用
DataInputStream
读取行 ,使用
LineNumberInputStream
来监控行。
从这个子可以看出,
嵌套流中数据的流动贯穿所有的流,
即所以流中的数据在同步流动。
1.8 PushbackInputStream
PushbackInputStream
也是从
FilterInputStre am
继承下来,它有一个特殊的方法
unread
,
用于将读出来的数据放出 流中。在例子
readLine
就使用了这个
方法,
unread
与
read
相
对应有三个形态:
unread()
、
unread(byte[])
及
unread(byte[],int,int)
。
1.9 PipedInputStream
FilterInputStream
的
派
生
类
已
介
绍
完
毕
,
下
面
接
着
介
绍
InputStream
的
派
生
类
。
Pipe dInputStream
一般与
PipedOutputStream
一起用构
成线程之间双向的通信管道。
因此
在里先不介绍
PipedInput Stream
的用法。
1.10 StringBufferInputStream
String buffer =
InputStream s = new StringBufferInputStream(buffer);
1.11 ByteArrayInputStream
ByteArrayInputStream
与
StringBufferInputStream
类
似
,
只
是
它
基
于
ByteArry
,
而
StringB ufferInputStream
基于
String
。
1.12 SequenceInputStream
SequenceInputStre am
用
于
将
不
同的
InputStream
按先
后
顺序
串在
一
起,
如将
两
个
InputStream
串起来:
InputStream s1 = new FileInputStream(
InputStream s2 = new FileInputStream(
InputStream s = new SequenceInputStream(s1, s2);
以上只能实现两个输入流的 串接,要实现两个以上输入流串接,需要用到
V
ector
类,如
下所示:< br>
Vector v = new Vector(3);
Enumeration e;
ment(s1);
ment(s2);
ment(s2);
e = ts();
InputStream s = new SequenceInputStream(e)
1.13 OutputStream
OutputStream
位于
Output
Stream
类层次的最顶层,它是一个抽象类,它规定了
Output
Stream
类的基本功能。
1.13.1 write
-
-
-
-
-
-
-
-
本文更新与2021-01-24 14:16,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/561068.html
-
上一篇:c语言程序设计何钦铭颜晖第12章文件
下一篇:首经贸 国际金融学(双语)试卷