CodeGym /課程 /Java 核心 /標準接口:InputStream、OutputStream

標準接口:InputStream、OutputStream

Java 核心
等級 3, 課堂 8
開放

“你好,阿米戈!現在我給你介紹兩個接口:InputStream和OutputStream。它們被聲明為抽像類,但如果你深入挖掘,你會發現它們本質上是接口。它們幾乎所有的方法都是抽象的,除了一些微不足道的手段,很像我們之前考慮的“保鏢”。

這些都是非常有趣的界面。現在,我將特意將它們稱為接口,以便您了解我們為什麼需要它們。然後我們將討論為什麼它們實際上是抽像類。

“好的。那麼這些接口是什麼?”

“我不多說了就告訴你。”

Java 有一個有趣的東西叫做“流”。流是一個非常簡單的實體。它的簡單性是一種非常強大的數據交換方式的關鍵。有兩種流:讀流和寫流。

正如您可能已經猜到的那樣,您可以將數據寫入流以進行寫入。它有一個寫方法。您可以從流中讀取數據以進行 reading 。為此,它有一個read () 方法。

InputStream 是支持讀取的流的接口。它定義了以下能力:«可以從我這裡讀取字節»。

同樣,OutputStream, OutputStream 是支持寫入的流的接口。它定義了以下能力:«字節可以寫給我»。

“就是這樣?”

“差不多。但重點是 Java 有大量的類可以與InputStream和OutputStream一起工作。例如,你想從磁盤讀取文件並將其內容顯示在屏幕上。沒有比這更容易的了。”

要從磁盤上的文件中讀取數據,我們有特殊的FileInputStream類,它實現了InputStream接口。您要將此數據寫入另一個文件嗎?為此,我們有FileOutputStream類,它實現了OutputStream接口。以下代碼顯示了將數據從一個文件複製到另一個文件需要執行的操作。

代碼
public static void main(String[] args) throws IOException
{
 InputStream inStream = new FileInputStream("c:/source.txt");
 OutputStream outStream = new FileOutputStream("c:/result.txt");

 while (inStream.available() > 0)
 {
  int data = inStream.read(); //read one byte from the input stream
  outStream.write(data); //write that byte to the other stream.
 }

 inStream.close(); //close the streams
 outStream.close();
}

想像一下,我們已經編寫了一個類並向 其添加了InputStream 和 OutputStream功能。

如果我們正確地實現了這些接口,那麼我們類的實例現在可以保存到文件或從文件中讀取。只需使用read方法讀取其內容即可。或者它們可以通過創建一個對象並使用 write 方法寫入文件內容來從文件中加載。

“也許是一個例子?”

“當然。”

代碼 描述
class MyClass
{
private ArrayList<Integer> list;
}
為簡單起見,假設我們的類包含一個對象,一個包含整數的 ArrayList。

現在我們將為其添加讀寫方法

代碼 描述
class MyClass
{
private ArrayList<Integer> list;
public void write(int data)
{
list.add(data);
}
public int read()
{
int first = list.get(0);
list.remove(0);
return first;
}

public int available()
{
return list.size();
}
}
現在我們的類實現了read方法,這使得順序讀取list的全部內容成為可能。

和write方法,它允許您將值寫入我們的列表。

當然,這不是 InputStream 和 OutputStream 接口的實現,但非常相似。

“是的,我明白了。那麼你如何將這樣一個對象的內容保存到文件中呢?”

“讓我給你舉個例子:”

將 MyClass 對象寫入文件
public static void main(String[] args)
{
 MyClass myObject = new MyClass();
 OutputStream outStream = new FileOutputStream ("c:/my-object-data.txt");

 while (myObject.available() > 0)
 {
  int data = myObject.read(); //read one int from the input stream
  outStream.write(data); //write that int to the other stream.
 }

 outStream.close();
}
從文件中讀取 MyClass 對象
public static void main(String[] args)
{
 InputStream inStream = new FileInputStream("c:/my-object-data.txt");
 MyClass myObject = new MyClass();

 while (inStream.available() > 0)
 {
  int data = inStream.read(); //read one int from the input stream
  myObject.write(data); //write that int to the other stream.
 }

 inStream.close(); //close the streams
}

“我的天啊!它真的與使用 InputStream/OutputStream 非常相似。流非常棒!”

“然後還有一些!”

3
任務
Java 核心, 等級 3, 課堂 8
上鎖
Code entry
Sometimes you don't need to think, you just need to hammer it out! As paradoxical as it may seem, sometimes your fingers will "remember" better than your conscious mind. That's why while training at the secret CodeGym center you will sometimes encounter tasks that require you to enter code. By entering code, you get used to the syntax and assimilate some material. What's more, you combat laziness.
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION