Java.io.FilterWriter.close() 方法



描述

java.io.FilterWriter.close() 方法關閉字串,並首先重新整理它。一旦流關閉,進一步呼叫 write() 或 flush() 將丟擲 IOException。

宣告

以下是 java.io.FilterWriter.close() 方法的宣告:

public void close()

引數

返回值

該方法不返回值。

異常

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

示例

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

package com.tutorialspoint;

import java.io.FilterWriter;
import java.io.StringWriter;
import java.io.Writer;

public class FilterWriterDemo {
   public static void main(String[] args) throws Exception {
      FilterWriter fw = null;
      Writer w = null;
      String s = null;
      
      try {
         // create new reader
         w = new StringWriter(6);
          
         // filter writer
         fw = new FilterWriter(w) {
         };
         
         // write to filter writer
         fw.write(65);
         
         // get the string
         s = w.toString();
         
         // print
         System.out.println("String: "+s);
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(w!=null)
            w.close();
         if(fw!=null) {
            fw.close();
            System.out.println("close() invoked");
            System.out.print("flushes out and then closes the stream");
         }
      }
   }
}

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

String: A
close() invoked
flushes out and then closes the stream
java_io_filterwriter.htm
廣告

© . All rights reserved.