Java-io-bufferedwriter-write-char

提供:Dev Guides
移動先:案内検索

Java.io.BufferedWriter.write()メソッド

説明

  • java.io.BufferedWriter.write(int c)*メソッドは、単一の文字を書き込みます。

宣言

以下は、* java.io.BufferedWriter.write(int c)*メソッドの宣言です。

public void write(int c)

パラメーター

  • c -書き込む文字を指定する整数

戻り値

このメソッドは値を返しません。

例外

  • IOException --I/Oエラーが発生した場合。

次の例は、java.io.BufferedWriter.write(int c)メソッドの使用方法を示しています。

package com.finddevguides;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class BufferedWriterDemo {
   public static void main(String[] args) throws IOException {

      StringWriter sw = null;
      BufferedWriter bw = null;

      try{
        //create string writer
         sw = new StringWriter();

        //create buffered writer
         bw = new BufferedWriter(sw);

        //integer range represents alphabets in uppercase
         for(int i = 65; i<=90; i++)
         {
            bw.write(i);
         }

        //forces out the characters to string writer
         bw.flush();

        //string buffer is created
         StringBuffer sb = sw.getBuffer();

        //prints the string
         System.out.println(sb);

      }catch(IOException e){
        //if I/O error occurs
         e.printStackTrace();
      }finally{
        //releases any system resources associated with the stream
         if(sw!=null)
            sw.close();
         if(bw!=null)
            bw.close();
      }
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます。

ABCDEFGHIJKLMNOPQRSTUVWXYZ