Java.io.Writer.close() 方法



描述

java.io.Writer.close() 方法關閉流,首先將其重新整理。流關閉後,進一步的 write() 或 flush() 呼叫將引發 IOException。關閉已關閉的流沒有效果。

宣告

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

public abstract void close()

引數

返回值

此方法不返回值。

異常

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

示例

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

package com.tutorialspoint;

import java.io.*;

public class WriterDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // append a string
         writer.append(s);

         // flush the writer
         writer.flush();

         // append a new string
         writer.append("\nThis is an example");

         // flush and close the stream
         writer.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

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

Hello World
This is an example
java_io_writer.htm
廣告
© . All rights reserved.