2009-12-02 224 views

回答

168

InputStreamOutputStream的目標是抽象不同的方式來輸入和輸出:流是一個文件,網頁或屏幕應該沒有關係。重要的是,您從流中接收信息(或將信息發送到該流中)。

InputStream用於許多您閱讀的內容。

OutputStream用於許多您寫信給的東西。

下面是一些示例代碼。它假定InputStream instrOutputStream osstr已創建:

int i; 

while ((i = instr.read()) != -1) { 
    osstr.write(i); 
} 

instr.close(); 
osstr.close(); 
+41

什麼是「流」? – 2014-06-28 15:44:18

+42

@KorayTugay流通常被定義爲一組字符。更確切地說,不止一個位或字符被稱爲流。 – 2014-07-28 15:21:25

9

您從InputStream中讀取並寫入OutputStream。例如

例如,要複製文件。您將創建一個FileInputStream來讀取源文件,並創建一個FileOutputStream以寫入新文件。

如果您的數據是字符流,您可以使用FileReader而不是InputStream和FileWriter,而不是使用OutputStream。

InputStream input = ... // many different types 
OutputStream output = ... // many different types 

byte[] buffer = new byte[1024]; 
int n = 0; 
while ((n = input.read(buffer)) != -1) 
    output.write(buffer, 0, n); 

input.close(); 
output.close(); 
+3

'close'總是'flush'es,所以沒有。 – pstanton 2012-12-14 09:10:14

5

OutputStream是一個表示寫入輸出的抽象類。有許多不同的OutputStream類,它們寫出某些東西(如屏幕,文件或字節數組或網絡連接等)。 InputStream類訪問相同的東西,但他們從中讀取數據。

下面是使用FileOutputStream中和的FileInputStream將數據寫入一個文件,然後讀回在a good basic example

80

的InputStream用於讀取,爲OutputStream進行寫入。它們作爲裝飾器相互連接,以便您可以讀取/寫入來自所有不同類型源的所有不同類型的數據。

例如,您可以原始數據寫入文件:

File file = new File("C:/text.bin"); 
file.createNewFile(); 
DataOutputStream stream = new DataOutputStream(new FileOutputStream(file)); 
stream.writeBoolean(true); 
stream.writeInt(1234); 
stream.close(); 

讀取寫入內容:

File file = new File("C:/text.bin"); 
DataInputStream stream = new DataInputStream(new FileInputStream(file)); 
boolean isTrue = stream.readBoolean(); 
int value = stream.readInt(); 
stream.close(); 
System.out.printlin(isTrue + " " + value); 

您可以使用其他類型的流,以提高讀/寫。例如,可以引入一個緩衝器,用於效率:

DataInputStream stream = new DataInputStream(
    new BufferedInputStream(new FileInputStream(file))); 

可以寫入其它的數據,如對象:

MyClass myObject = new MyClass(); // MyClass have to implement Serializable 
ObjectOutputStream stream = new ObjectOutputStream(
    new FileOutputStream("C:/text.obj")); 
stream.writeObject(myObject); 
stream.close(); 

可以從其它不同的輸入源讀:

byte[] test = new byte[] {0, 0, 1, 0, 0, 0, 1, 1, 8, 9}; 
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(test)); 
int value0 = stream.readInt(); 
int value1 = stream.readInt(); 
byte value2 = stream.readByte(); 
byte value3 = stream.readByte(); 
stream.close(); 
System.out.println(value0 + " " + value1 + " " + value2 + " " + value3); 

對於大多數輸入流,也有一個輸出流。你可以定義你自己的流來讀/寫特殊的東西,並且有複雜的流來閱讀複雜的東西(例如有讀取/寫入ZIP格式的流)。

22

Java Tutorial

流是數據的序列。

的程序使用輸入流,以在一個時間從源讀出的數據,一個項目:

enter image description here

甲程序使用的輸出流數據寫入到目的在時間,一個項目:

enter image description here

上圖中的數據源和數據目標可以是保存,生成或使用數據的任何內容。顯然這包括磁盤文件,但源或目的地也可以是其他程序,外圍設備,網絡套接字或陣列

樣品code從Oracle教程:

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

public class CopyBytes { 
    public static void main(String[] args) throws IOException { 

     FileInputStream in = null; 
     FileOutputStream out = null; 

     try { 
      in = new FileInputStream("xanadu.txt"); 
      out = new FileOutputStream("outagain.txt"); 
      int c; 

      while ((c = in.read()) != -1) { 
       out.write(c); 
      } 
     } finally { 
      if (in != null) { 
       in.close(); 
      } 
      if (out != null) { 
       out.close(); 
      } 
     } 
    } 
} 

此程序使用字節流文件複製xanadu.txt到outagain.txt通過一次寫一個字節

查看此SE問題,以瞭解有關高級字符流的更多詳細信息,這些字符流是位於字節流頂部的包裝:

byte stream and character stream

0

的輸出流一般涉及像文件或網絡etc.In java的輸出流的一些數據的目的地是一個目的地,其中數據寫入最終和它結束

import java.io.printstream; 

class PPrint { 
    static PPrintStream oout = new PPrintStream(); 
} 

class PPrintStream { 
    void print(String str) { 
     System.out.println(str) 
    } 
} 

class outputstreamDemo { 
    public static void main(String args[]) { 
     System.out.println("hello world"); 
     System.out.prinln("this is output stream demo"); 
    } 
} 
0

:從外行看,流是數據,大多數通用流是數據的二進制表示。

輸入流:如果您正在從文件或任何其他來源讀取數據,則使用的流是輸入流。在一個簡單的術語中,輸入流用作讀取數據的通道。

輸出流:如果你想閱讀並從源(文件等)的過程數據首先需要保存數據,存儲數據的平均值是輸出流。

0

流是液體,空氣或氣體的連續流。

Java流是從源或到目的地的數據流。源或目標可以是磁盤,內存,套接字或其他程序。數據可以是字節,字符或對象。這同樣適用於C#或C++流。爪哇溪流的一個很好的比喻是水從水龍頭流入浴缸,然後流入排水溝。

數據表示流的靜態部分;讀寫方法是流的動態部分。

InputStream表示數據從源流動,所述OutputStream表示數據的流動到目標。 最後,InputStreamOutputStream是抽象的過度低級別的訪問到的數據,如C文件指針。

相關問題