java.io.Writer.write(int c)方法範例


java.io.Writer.write(int c) 方法寫入單個字元。要寫入的字元被包含在給定整數值的低16位元; 16個高序位被忽略。

宣告

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

public void write(int c)

引數

  • c -- int,指定一個寫入的字元

返回值

此方法沒有返回值

異常

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

例子

下面的範例演示java.io.Writer.write()方法的用法。

package com.yiibai;

import java.io.*;

public class WriterDemo {

   public static void main(String[] args) {
      int c = 70;

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

      try {
         // write an int that will be printed as ASCII
         writer.write(c);

         // flush the writer
         writer.flush();

         // write another int that will be printed as ASCII
         writer.write(71);

         // flush the stream again
         writer.flush();

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

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

FG