Java.io.OutputStream.close() 方法



描述

java.io.OutputStream.close() 方法關閉此輸出流並釋放與此流關聯的任何系統資源。close 的一般約定是它關閉輸出流。關閉的流無法執行輸出操作,也無法重新開啟。OutputStream 的 close 方法不執行任何操作。

宣告

以下是java.io.OutputStream.close() 方法的宣告。

public void close()

引數

返回值

此方法不返回值。

異常

IOException − 如果發生 I/O 錯誤。

示例

以下示例演示了java.io.OutputStream.close() 方法的使用。

package com.tutorialspoint;

import java.io.*;

public class OutputStreamDemo {
   public static void main(String[] args) {
      try {
         // create a new output stream
         OutputStream os = new FileOutputStream("test.txt");

         // craete a new input stream
         InputStream is = new FileInputStream("test.txt");

         // write something
         os.write('A');

         // flush the stream
         os.flush();

         // close the stream but it does nothing
         os.close();

         // read what we wrote
         System.out.println("" + (char) is.read());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

A
java_io_outputstream.htm
廣告
© . All rights reserved.