天天看點

java inputstream位元組流_Java常用輸入位元組流InputStream

輸入位元組流InputStream的一系列實作,以下是五種較常用的子類

1、位元組數組作為輸入源——ByteArrayInputStream

該類包括兩個構造方法

ByteArrayInputStream(byte[] buf);

ByteArrayInputStream(byte[] buf,int offset,int length );

該類重寫了InputStream中的所有方法,是以我們可以調用其父類同名的方法進行讀寫操作。

下面是如何通過一個位元組數組建立位元組輸入流的過程,并從輸入流中讀取資料輸出到操作台。

importJava.io.ByteArrayInputStream;importjava.io.IOException;public classTestByteArrayInputStream {public static void main(String[] args) throwsIOException {//初始化位元組數組

byte[] buf = new byte[3];

buf[0] = 100;

buf[1] = 101;

buf[2] = 102;//建立輸入流

ByteArrayInputStream input = newByteArrayInputStream(buf);//從輸入流中讀取資料

byte[] out = new byte[3];

input.read(out);

System.out.println(newString(out));//關閉輸入流

input.close();

}

}

2、檔案作為輸入源——FileInputStream

FileInputStream從檔案系統中的某個檔案中擷取輸入位元組,适用于讀取諸如圖像資料之類的原始資料流。有一下兩種建立方法:

FileInputStream(File file);//通過系統中的File對象file指定

FileInputStream(String name);//通過系統中路徑名name指定

同樣的我們使用重寫的InputStream中的方法的名稱來實作讀檔案内容。

下面是如何讀取磁盤檔案來建立一個檔案輸入流,循環讀取該資料流中的資料并輸資料到控制台。

importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;public classTestFileInputStream {public static void main(String[] args) throwsFileNotFoundException {try{//建立輸入流

FileInputStream input = new FileInputStream("D:/demo/test.txt");//從輸入流中讀取資料

while (input.available() > 0) {int out =input.read();

System.out.println((char) out);

}//關閉輸入流

input.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

3、對象作為輸入源——ObjectInputStream

ObjectInputStream與FileInputStream一起使用時,可以為應用程式提供對對象圖形的持久儲存。

importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.ObjectInputStream;importjava.sql.Date;public classTestObjectInputStream {public static void main(String[] args) throwsClassNotFoundException {try{//建立檔案輸入流

FileInputStream file = new FileInputStream("D:/demo?test.tmp");//建立對象輸入流

ObjectInputStream object = newObjectInputStream(file);//讀取對象的資料

int i =object.readInt();

String string=(String) object.readObject();

Date date=(Date) object.readObject();

System.out.println("i=" +i);

System.out.println("string=" +string);

System.out.println("date=" +date);//關閉流

object.close();

file.close();

}catch(FileNotFoundException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

4、字元串作為輸入源——StringBufferInputStream

StringBufferInputStream允許輸入流由字元串内容提供,與ByteArrayInputStream使用位元組數組作為輸入源類似,但是隻有一種構造方法:

StringBufferInputStream(String s); //以從指定字元串讀取資料

隻需要一個字元串作為輸入就可以建立,以下是一個執行個體:

importjava.io.IOException;importjava.io.StringBufferInputStream;public classTestStringBufferInputStream {public static voidmain(String[] args) {//建立輸入流

StringBufferInputStream input = new StringBufferInputStream("Hello World!");//從輸入流中讀取資料

while (input.available() > 0) {int out =input.read();

System.out.print((char) out);

}//關閉輸入流

try{

input.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

5、緩存輸入流——BufferedInputStream

BufferedInputStream為另一個輸入流添加一些功能,即緩存輸入功能。在建立BufferedInputStream時,會建立一個内部緩存區間數組。在讀取或者跳過流中位元組時,可根據需要從包含的輸入流在此填充該内部緩存區,一次填充多個位元組。

如���你需要一個具有緩存的檔案輸入流,則應當組合使用FileInputStream和BufferedInputStream,這将能提高讀取效率。

下面一段代碼是将檔案輸入流對象轉換成緩存輸入流的過程:

importjava.io.BufferedInputStream;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;public classTestBufferedInputStream {private voidmian() {//TODO Auto-generated method stub

try{//建立檔案輸入流

FileInputStream input = new FileInputStream("D:/demo/test.txt");

BufferedInputStream buffer= newBufferedInputStream(input);//從輸入流中讀取資料

while (buffer.available() > 0) {int out =buffer.read();

System.out.print((char) out);

}//關閉流

buffer.close();

input.close();

}catch(FileNotFoundException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

java inputstream位元組流_Java常用輸入位元組流InputStream