當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.io.Writer.write()用法及代碼示例


描述

這個java.io.Writer.write(int c)方法寫入單個字符。要寫入的字符包含在給定整數值的低 16 位中; 16 個 high-order 位被忽略。

聲明

以下是聲明java.io.Writer.write()方法。

public void write(int c)

參數

c- int 指定要寫入的字符。

返回值

此方法不返回值。

異常

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

示例

下麵的例子展示了使用java.io.Writer.write()方法。

package com.tutorialspoint;

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

相關用法


注:本文由純淨天空篩選整理自 Java.io.Writer.write() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。