1. Streams及I/O
Stream就是信息源與目的地之間的通信路徑,這里的信息源可以是文件、內(nèi)存、
網(wǎng)絡(luò)
等。Streams主要分為input及output Stream。
1.1 InputStream類
類InputStream處于Input stream類層次的最頂層,它主要具有以下幾種方法:
1.1.1 read方法
read方法用于從指定的輸入流讀取以字節(jié)為單位的數(shù)據(jù),第一次從流的開始位置開始讀取,以后每次從上次的結(jié)束部位開始讀取,即自動(dòng)實(shí)現(xiàn)了位移。
read方法有以下三種形態(tài):
(1) int read(byte buff[n]):從指定輸入流中讀取n個(gè)字節(jié)填充到buff中,該方法返回讀取的實(shí)際字節(jié)數(shù),如果讀取的實(shí)際字節(jié)數(shù)小于n,一般是因?yàn)橐炎x到指定輸入流的末尾;
(2) int read():即不帶參數(shù),該方法每次一個(gè)字節(jié)從指定的輸入流中讀取數(shù)據(jù)。返回值也是int類型,但它并不代表讀取的字節(jié)數(shù),而是從流中讀取的數(shù)據(jù)的本身,因數(shù)據(jù)本身是byte類型的,所以一般要強(qiáng)制進(jìn)行轉(zhuǎn)化;如果讀到流的末尾返回的值是-1;
(3) int read(byte buff[n],int start, int len):從指定流讀取數(shù)據(jù),從start開始,填充len個(gè)字節(jié)到buff中,返回值為實(shí)際的填充數(shù),如果返回值<len,一般表示已將指定流中的數(shù)據(jù)讀完;
以下是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),該方法使指定流中的當(dāng)前位置移動(dòng)n個(gè)字節(jié),n的值可以是負(fù)值用于向前移,skip方法返回值為實(shí)際移動(dòng)的字節(jié)數(shù),由于種種原因,如已到流尾或者其它原因返回的值往往小于n。對于讀取文件來說,小于n的原因最大的原因是讀到了文件尾。
1.1.3 available方法
available方法用于計(jì)算指定流中當(dāng)前有多少字節(jié),如果指定的流是文件流,那么就返回文件的大小。Available返回的值是int類型。
有的輸入流可能沒有能力返回字節(jié)數(shù),如果對這些輸入流使用avaiable方法,返回值為0。
1.1.4 close方法
對于打開的stream,Java可以自動(dòng)回收,但是Java自動(dòng)回收需要時(shí)間,所以最好自己調(diào)用close方法來關(guān)閉stream,這樣方便下一次重新指定的流。
1.2 ByteArrayInputStream
ByteArrayInputStream是從InputStream中繼承下來的,用于從字節(jié)數(shù)組中提取數(shù)據(jù),關(guān)于ByteArrayInputStream的創(chuàng)建例子如下:
byte[] buffer = new byte[1024];
fillWithUsefulData(buffer); //自定義的方法,用于在buffer中填充數(shù)據(jù)
InputStream s = new ByteArrayInputStream(buffer);
InputStream s1 = new ByteArrayInputStream(buffer,100,300);
其中ByteArrayInputStream(buffer,100,300)是創(chuàng)建到buffer的stream,從buffer的第100個(gè)字節(jié)開始取300字節(jié)。
ByteArrayInputStream的其它方法與InputStream類似,這里不再重復(fù)。
1.3 FileInputStream
FileInputStream也是從InputStream中繼承下來的,用于從指定的文件中提取。因此它的方法也與InputStream中的方法類似,這里不再介紹,只介紹FileInputStream中特殊的方法:getFD(),該方法用于獲取文件句柄。使用方法如下:
FileInputStream aFIS = new FileInputStream("aFileName");
FileDescriptor myFD = aFIS.getFD();
這樣以后要用到aFileName文件時(shí)可以使用myFD這個(gè)文件句柄(實(shí)際上是文件描述類的實(shí)例),如要重新打開該文件,可以使用FileInputStream aFIS = new FileInputStream(myFD)。
關(guān)于文件描述類FileDescriptor,有以下幾點(diǎn)說明:
(1) 屬性in:標(biāo)準(zhǔn)輸入;
(2) 屬性out:標(biāo)準(zhǔn)輸出;
(3) 屬性err:標(biāo)準(zhǔn)錯(cuò)誤輸出;
在FileInputStream中還有另一個(gè)特殊的方法就是:finalize()。
1.4 FilterInputStream
FilterInputStream也是從InputStream中繼承下來,不過FilterInputStream類基本上不能直接使用,一般上使用該類的派生類,如BufferedInputStream等。該類的最大特點(diǎn)是,在定義時(shí)可以嵌套:
InputStream s = getAnInputStreamFromSomewhere();
FilterInputStream s1 = new FilterInputStream(s);
FilterInputStream s2 = new FilterInputStream(s1);
FilterInputStream s3 = new FilterInputStream(s2);
所以該類的所有派生類都具有這個(gè)特性。
1.5 BufferedInputStream
BufferedInputStream指定數(shù)據(jù)源是內(nèi)存的指定區(qū)域,從FilterInputStream繼承下來的,這種類型的Stream主要用于提高性能,它定義時(shí)一般指定其它的InputStream,如:
InputStream s = new BufferedInputStream(new FileInputStream("foo"));
BufferedInputSream是可以使用mark及reset方法,使用上述的嵌套方法間接的使其它的stream也支持這些方法了。
由于BufferedInputStream需要buffer,所以它需要等待在它之前的數(shù)據(jù)都到了后才工作,所以BufferedInputStream最好用在流的前面,如上面這個(gè)例子,當(dāng)然用在最前面也沒有什么意思了。
1.6 DataInputStream
DataInputStream也是從FilterInputStream繼承下來的,所以也具有父類的特性。DataInputStream還implement DataInput接口,所以DataInputStream具體的方法最多,如:readShort、readBoolean、readByte、readUnsignedByte、readShort等。這些方法的都是read方法的擴(kuò)展,使用上也相似,所以這里不多介紹。
以下是readInt的實(shí)現(xiàn):
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的實(shí)現(xiàn):
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);
}
在這個(gè)例子中,如果讀出的字符中\(zhòng)r,還得再讀一位判斷是否是\n,如果不是\n,還得將這個(gè)字符放回到輸入流中,因此使用了PushbackInputStream的功能。
如果對DataInputStream的讀操作已到Stream的末尾,會(huì)拋出EOFException異常。在Stream末尾,skipBytes不做任何動(dòng)作;readLine返回null;readUTF拋出UTFDataFormatException異常。
1.7 LineNumberInputStream
同樣LineNumberInputStream是從FilterInputStream繼承下來的,該類可以跟蹤行號,設(shè)置行號,對行作標(biāo)記以便恢復(fù)等功能。一般不直接使用該類,而使用其它Stream的嵌套間接對它進(jìn)行使用,如:
LineNumberInputStream aLNIS;
aLNIS = new LineNumberInputStream(new FileInputStream("source"));
DataInputStream s = new DataInputStream(aLNIS);
String line;
while ((line = s.readLine()) != null) {
. . . // process the line
System.out.println("Did line number: " + aLNIS.getLineNumber());
}
在這個(gè)例子中,使用DataInputStream讀取行,使用LineNumberInputStream來監(jiān)控行。從這個(gè)子可以看出,嵌套流中數(shù)據(jù)的流動(dòng)貫穿所有的流,即所以流中的數(shù)據(jù)在同步流動(dòng)。
1.8 PushbackInputStream
PushbackInputStream也是從FilterInputStream繼承下來,它有一個(gè)特殊的方法unread,用于將讀出來的數(shù)據(jù)放出流中。在例子readLine就使用了這個(gè)方法,unread與read相對應(yīng)有三個(gè)形態(tài):unread()、unread(byte[])及unread(byte[],int,int)。
1.9 PipedInputStream
FilterInputStream的派生類已介紹完畢,下面接著介紹InputStream的派生類。PipedInputStream一般與PipedOutputStream一起用構(gòu)成線程之間雙向的通信管道。因此在里先不介紹PipedInputStream的用法。
1.10 StringBufferInputStream
String buffer = "Now is the time for all good men to come...";
InputStream s = new StringBufferInputStream(buffer);
1.11 ByteArrayInputStream
ByteArrayInputStream與StringBufferInputStream類似,只是它基于ByteArry,而StringBufferInputStream基于String。
1.12 SequenceInputStream
SequenceInputStream用于將不同的InputStream按先后順序串在一起,如將兩個(gè)InputStream串起來:
InputStream s1 = new FileInputStream("theFirstPart");
InputStream s2 = new FileInputStream("theRest");
InputStream s = new SequenceInputStream(s1, s2);
以上只能實(shí)現(xiàn)兩個(gè)輸入流的串接,要實(shí)現(xiàn)兩個(gè)以上輸入流串接,需要用到Vector類,如下所示:
Vector v = new Vector(3);
Enumeration e;
v.addElement(s1);
v.addElement(s2);
v.addElement(s2);
e = v.elements();
InputStream s = new SequenceInputStream(e) ;
1.13 OutputStream
OutputStream位于Output Stream類層次的最頂層,它是一個(gè)抽象類,它規(guī)定了Output Stream類的基本功能。
1.13.1 write
write方法與InputStream的read方法相對應(yīng),它有三個(gè)形態(tài):
(1) write(byte[]):將指定byte數(shù)組中的數(shù)據(jù)輸出到指定Stream;
(2) write(byte[],int,int):將指定byte數(shù)組中的數(shù)據(jù)從第二個(gè)參數(shù)開始,輸出第三個(gè)參數(shù)指定的長度到指定的Stream;
(3) wirte(int);將一個(gè)int值輸出到指定的Stream;
1.13.2 flush和close
有些輸出流在輸出時(shí)先放在緩沖中,可以使用flush將這些數(shù)據(jù)真正寫入指定的輸出流中。Close用于關(guān)閉指定的輸出流。
1.14 ByteArrayOutputStream
ByteArrayOutputStream將一個(gè)輸出流指向一個(gè)Byte數(shù)組,但這個(gè)Byte數(shù)組是ByteArrayOutputStream內(nèi)部內(nèi)置的,不需要我們來定義。該類有兩個(gè)構(gòu)造函數(shù):
(1) ByteArrayOutputStream():該構(gòu)造函數(shù)會(huì)在內(nèi)部創(chuàng)建一個(gè)長度為32的Byte數(shù)組;
(2) ByteArrayOutputStream(int n):在對象內(nèi)部創(chuàng)建一個(gè)長度為n的Byte數(shù)組。
ByteArrayOutputStream從OutputStream類繼承下來,所以它具write、flush及close等方法,同時(shí)它還具有以下幾個(gè)特殊的方法:
(3) toString():將對象內(nèi)部的Byte數(shù)組轉(zhuǎn)化成字符串(String(buf,0,count);
(4) toString(int n):將對象對部的Byte數(shù)組轉(zhuǎn)化成字符串,編碼方式為n;
(5) toString(String n):將對象對部的數(shù)組轉(zhuǎn)化成字符串,編碼方式為n;
(6) toByteArray():返回對象內(nèi)部的Byte數(shù)組;
(7) writeTo(OutputStream):將內(nèi)部Byte數(shù)組輸出到指定的輸出流;
(8) reset():將對象內(nèi)部Byte數(shù)組的長度設(shè)為0,{count = 0};
(9) size():返回Byte數(shù)組長度;
1.15 FileOutputStream
FileOutputStream與FileInputStream相對應(yīng),它具有以下幾個(gè)構(gòu)造函數(shù):
(1) FileOutputStream(File)
(2) FileOutputStream(File file, boolean append):如果append為真,以添加方式寫入文件,如果為否(缺省),以新增方式寫入文件;
(3) FileOutputStream(FileDescriptor)
(4) FileOutputStream(String name)
(5) FileOutputStream(String name, boolean append)
其它的方法大多為標(biāo)準(zhǔn)方法,這里不再介紹。以下使用的例子:
FileDescriptor fd = someFileStream.getFD();
OutputStream s = new FileOutputStream(fd);
1.16 FilterOutputStream
FilterOutputStream與FilterInputStream相對應(yīng),使用方法也相類似。
1.17 BufferedOutputStream
BufferedOutputStream從FilterOutputStream中繼承下來,它與BufferedInputStream相對應(yīng),作用也相類似,它主要為輸出流做緩沖,如:
OutputStream s = new BufferedOutputStream(new FileOutputStream("foo"));
由于BufferedOutputStream是緩沖數(shù)據(jù)的,所以必要時(shí),需要使用flush方法強(qiáng)制將緩沖中的數(shù)據(jù)真正寫入輸出流中。
1.18 DataOutputStream
DataOutputStream與DataInputStream相對應(yīng),在繼承OutputStream的同時(shí),實(shí)現(xiàn)了DataOutput接口,因此它具有DataOutput接中所規(guī)定的方法,這些方法與DataInput所規(guī)定的方法相反。
它還具有size方法,該方法返回向輸出流寫入的字節(jié)數(shù)。以下是實(shí)現(xiàn)復(fù)制功能的例子:
try { while (true) aDO.writeByte(aDI.readByte()); }
finally { aDI.close(); aDO.close(); }
1.19 PrintStream
PrintStream是從FilterOutputStream繼承下來的。使用例子如下:
PrintStream s = new PrintStream(new FileOutputStream("foo"));
s.println("Here's the first line of text in the file foo.");
這個(gè)例子說明可以使用PrintStream向文件寫數(shù)據(jù),并且該類提供了輸出行的功能,彌補(bǔ)了DataOutputStream的空白(在DataOutputStream沒有輸出行的功能)。
PrintStream的構(gòu)造函數(shù):
(1) PrintStream(boolean autoFlush, OutputStream out)
(2) PrintStream(OutputStream out)
(3) PrintStream(OutputStream out, boolean autoFlush)
(4) PrintStream(OutputStream out, boolean autoFlush, String encoding)
1.20 PipedOutputStream
PipedOutputStream與PipedInputSteam相互配合實(shí)現(xiàn)兩個(gè)線程之間的通信,它們的定義如下:
PipedInputStream sIn = PipedInputStream();
PipedOutputStream sOut = PipedOutputStream(sIn);
以下是使用例子,該例子接收標(biāo)準(zhǔn)輸入的數(shù)據(jù),并輸出到標(biāo)準(zhǔn)輸出:
import java.io.*;
class ReadThread extends Thread implements Runnable {
InputStream pi = null;
OutputStream po = null;
String process = null;
ReadThread( String process, InputStream pi, OutputStream po) {
this.pi = pi; this.po = po; this.process = process; }
public void run() {
int ch; byte[] buffer = new byte[12]; int bytes_read;
try {
for(;;) {
bytes_read = pi.read(buffer); //從指定流讀入數(shù)據(jù)
if (bytes_read == -1) { return; }
po.write(buffer, 0, bytes_read);//向指定流寫入數(shù)據(jù)
Thread.yield();
}
} catch (Exception e) { e.printStackTrace();
} finally { }
}
}
public class MyPipe{
public static void main( String [] args) {
try {
int ch;
PipedInputStream writeIn = new PipedInputStream();
PipedOutputStream readOut = new PipedOutputStream( writeIn );
FileOutputStream writeOut = new FileOutputStream("out");
ReadThread rt = new ReadThread("reader", System.in, readOut );
ReadThread wt = new ReadThread("writer", writeIn, System.out );
rt.start();
wt.start();
} catch (Exception e) {
e.printStackTrace();
} }}
說明:
(1) 類ReadThread非常巧妙,它并沒有指定輸入輸出流的具體類型
(2) 在MyPipe類中new ReadThread("reader", System.in, readOut )語句使得從標(biāo)準(zhǔn)輸入設(shè)備中接收數(shù)據(jù),而從readOut輸出,而readOut是PipedOutputSteam,所以它可以被另一線程接收;
(3) new ReadThread("writer", writeIn, System.out ),從writeIn接收數(shù)據(jù),writeIn是readOut是成對的雙向管道,它接收從readOut發(fā)送過來的數(shù)據(jù)。再從標(biāo)準(zhǔn)設(shè)備中輸出。
1.21 RandomAccessFile
1.22 StreamTokenizer
1.23 ObjectOutputStream
ObjectOutputStream從OutputStream繼承下來,并實(shí)現(xiàn)了ObjectOutput, ObjectStreamConstants這兩個(gè)接口。它負(fù)責(zé)將指定對象輸出到指定的輸出流,可以將非static、非transient的屬性及值,對象的類定義輸出到指定的輸出流。該類有一個(gè)非常用的方法:
writeObject (Object obj);
該方法將obj輸出到指定的輸出流,以下是該類的例子:
FileOutputStream f = new FileOutputStream("tmp");
ObjectOutput s = new ObjectOutputStream(f);
s.writeObject("Today");
s.writeObject(new Date());
s.flush();
可以使用transient修飾符規(guī)定一些變量的值不被輸出到指定的輸出流,如:
public transient int transientValue = 4;
這樣transientValue的值就不會(huì)被輸出到輸出流。
1.24 ObjectInputStream
ObjectInputStream與ObjectOutputStream相對應(yīng),它是將對象的值及類的定義等從指定的輸入流讀入,以便重新對象化:
FileInputStream in = new FileInputStream("tmp");
ObjectInputStream s = new ObjectInputStream(in);
String today = (String)s.readObject();
Date date = (Date)s.readObject();
ObjectOutputStream和ObjectInputStream就可以實(shí)現(xiàn)對象的持久化,即要先將對象序列化保存到介質(zhì)中,在必要的時(shí)候重新恢復(fù)這些對象。
posted on 2006-03-20 12:19
zjw_albert 閱讀(128)
評論(0) 編輯 收藏