Java.io.SequenceInputStream.close()方法實例
java.io.SequenceInputStream.close() 方法關閉此輸入流並釋放與該流關聯的所有系統資源。一個封閉的SequenceInputStream不能進行輸入操作,並無法重新打開。
聲明
以下是java.io.SequenceInputStream.close()方法的聲明
public void close()
參數
- NA
返回值
此方法不返回任何值。
異常
- IOException -- 如果發生I/O錯誤。
例子
下面的示例演示java.io.SequenceInputStream.close()方法的用法。
package com.yiibai; import java.io.*; public class SequenceInputStreamDemo { public static void main(String[] args) { // create two new strings with 5 characters each String s1 = "Hello"; String s2 = "World"; // create 2 input streams byte[] b1 = s1.getBytes(); byte[] b2 = s2.getBytes(); ByteArrayInputStream is1 = new ByteArrayInputStream(b1); ByteArrayInputStream is2 = new ByteArrayInputStream(b2); // create a new Sequence Input Stream SequenceInputStream sis = new SequenceInputStream(is1, is2); try { // read 10 characters, 5 from each stream for (int i = 0; i < 10; i++) { char c = (char) sis.read(); System.out.print("" + c); } // change line System.out.println(); // start closing the streams System.out.println("Closing Streams..."); // close the streams sis.close(); is1.close(); is2.close(); // print message for closed streams System.out.println("Streams Closed."); } catch (IOException ex) { ex.printStackTrace(); } } }
讓我們編譯和運行上面的程序,這將產生以下結果:
HelloWorld Closing Streams... Streams Closed.