目录

void close()

描述 (Description)

java.io.OutputStream.close()方法关闭此输出流并释放与此流关联的所有系统资源。 close的一般合同是它关闭输出流。 封闭流无法执行输出操作,无法重新打开。 OutputStream的close方法什么都不做。

声明 (Declaration)

以下是java.io.OutputStream.close()方法的声明。

public void close()

参数 (Parameters)

NA

返回值 (Return Value)

此方法不返回值。

异常 (Exception)

IOException - 如果发生I/O错误。

例子 (Example)

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

package com.iowiki;
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
↑回到顶部↑
WIKI教程 @2018