InputStream

InputStream

InputStream的作用是標誌那些從不同數據起源產生輸入的類。這些數據起源包括(每個都有一個相關的InputStream子類)。

基本介紹

  • 中文名:輸入流
  • 外文名:InputStream
  • 作用:用作標誌
  • 學科:計算機學
java的InputStream的類型,InputStream的使用方法,對輸入檔案作緩衝,讀取記憶體,讀取格式化的記憶體,讀取檔案,

java的InputStream的類型

InputStream的作用是標誌那些從不同數據起源產生輸入的類。這些數據起源包括(每個都有一個相關的InputStream子類):
(2) String對象
(3) 檔案
(4) “管道”,它的工作原理與現實生活中的管道類似:將一些東西一端置入,它們在另一端輸出。
(5) 一個由其他種類的流組成的序列,以便我們將其統一收集合併到一個流內。
(6) 其他數據集,如Internet連線等。
除此以外,FilterInputStream也屬於InputStream的一種類型,用它可為“破壞器”類提供一個基礎類,以便將屬性或者有用的接口同輸入流連線到一起。這將在以後討論。
ClassFunctionConstructor ArgumentsHow to use it
ByteArray-InputStream
Allows a buffer in memory to be used as an InputStream.
The buffer from which to extract the bytes.
As a source of data. Connect it to a FilterInputStreamobject to provide a useful interface.
StringBuffer-InputStream
Converts a Stringinto an InputStream.
A String. The underlying implementation actually uses a StringBuffer.
As a source of data. Connect it to a FilterInputStreamobject to provide a useful interface.
File-InputStream
For reading information from a file.
A Stringrepresenting the file name, or a Fileor FileDescriptorobject.
As a source of data. Connect it to a FilterInputStreamobject to provide a useful interface.
類 功能 構建器參數/如何使用
ByteArrayInputStream 允許記憶體中的一個緩衝區作為InputStream使用 從中提取位元組的緩衝區/作為一個數據源使用。通過將其同一個FilterInputStream對象連線,可提供一個有用的接口
StringBufferInputStream 將一個String轉換成InputStream 一個String(字串)。基礎的實施方案實際採用一個StringBuffer(字串緩衝)/作為一個數據源使用。通過將其同一個FilterInputStream對象連線,可提供一個有用的接口
FileInputStream 用於從檔案讀取信息 代表檔案名稱的一個String,或者一個File或FileDescriptor對象/作為一個數據源使用。通過將其同一個FilterInputStream對象連線,可提供一個有用的接口
Piped-InputStream
Produces the data that’s being written to the associated PipedOutput-Stream. Implements the “piping” concept.
PipedOutputStream
As a source of data in multithreading. Connect it to a FilterInputStreamobject to provide a useful interface.
Sequence-InputStream
Coverts two or more InputStreamobjects into a single InputStream.
Two InputStreamobjects or an Enumerationfor a container of InputStreamobjects.
As a source of data. Connect it to a FilterInputStreamobject to provide a useful interface.
Filter-InputStream
Abstract class which is an interface for decorators that provide useful functionality to the other InputStreamclasses. See Table 10-3.
See Table 10-3.
See Table 10-3.
PipedInputString 產生為相關的PipedOutputStream寫的數據。實現了“管道化”的概念 PipedOutputStream/作為一個數據源使用。通過將其同一個FilterInputStream對象連線,可提供一個有用的接口
SequenceInputStream 將兩個或更多的InputStream對象轉換成單個InputStream使用 兩個InputStream對象或者一個Enumeration,用於InputStream對象的一個容器/作為一個數據源使用。通過將其同一個FilterInputStream對象連線,可提供一個有用的接口
FilterInputStream 對作為破壞器接口使用的類進行抽象;那個破壞器為其他InputStream類提供了有用的功能。參見表10.3

InputStream的使用方法

對輸入檔案作緩衝

要想打開檔案讀取字元,你得先用String或File對象創建一個FileInputReader。為了提高速度,你應該對這個檔案作緩衝,因此你得把FileInputReader的reference交給BufferedReader。由於BufferedReader也提供了readLine( )方法,因此它就成為你最終要使用的那個對象,而它的接口也成為你使用的接口了。當你讀到了檔案的末尾時,readLine( )會返回一個null,於是就退出while循環了。
String s2是用來累加檔案內容的(再加一個換行符,因為readLine( )會把它們都剝掉)。後面的程式都要用到這個s2。最後,用close( )來關閉檔案。單從技術角度上說,程式退出的時候(不管有沒有垃圾要回收)都應該調用finalize( ),而finalize( )又會調用close( )。不過各種JVM的實現並不一致,所以最好還是明確地調用close( )。
演示了如何用System.in來生成一個能讀取控制台輸入的流。System.in是一個InputStream,而BufferedReader需要一個Reader作參數,所以要先通過InputStreamReader來轉轉手。

讀取記憶體

現在String s2裡面已經有一個完整的檔案了。因此這部分程式要用它去創建一個StringReader。然後用(StringReader的)read( )方法把字元讀出來,再送到控制台上去。注意,read( )會把讀出來的byte當作int,所以要想正常列印的話,你得先把它們轉換成char。

讀取格式化的記憶體

要想讀取"格式化"的數據,你就得用DataInputStream了,它是一個面向byte的I/O類 (不是面向char的),因此你只能從頭到底一直用InputStream了。當然你可以把所有東西(比方說檔案) 都當成byte,然後用InputStream讀出來,但這裡是String。要想把String變成成byte數組,可以用String的getBytes( )方法,而ByteArrayInputStream是可以處理byte數組的。到了這一步,你就不用擔心沒有合適的InputStream來創建DataInputStream了。
如果你是用readByte( )逐位元組地讀取DataInputStream的話,那么無論byte的值是多少,都是合法的,所以你無法根據返回值來判斷輸入是否已經結束了。你只能用available( )來判斷還有多少字元

讀取檔案

這段程式還演示了怎樣往檔案裡面寫數據。先創建一個FileWriter。BufferedWriter總是免不掉的。(試試把BufferedWriter去掉,你就能看到它對性能的影響了—— 緩衝能大幅提高I/O的性能)。然後再讓PrintWriter去排版。這樣就能得出能夠讀得懂的,普通的文本檔案了。
隨著程式一行一行地寫檔案,我們就順便加了一個行號。注意,我們沒用LineNumberInputStream,因為這是一個傻乎乎的,沒什麼用的類。正如這裡所看到的,要知道行號還不簡單。
輸入流用完之後,readLine( )會返回null。你會發現,程式顯式地調用了out1的close( ),因為如果寫檔案的時候不調用close( ),它是不會去清空緩衝區的,這樣就有可能會落下一些東西了。
在JAVA中,InputStream是所有位元組輸入流的父類。

相關詞條

熱門詞條

聯絡我們