Java BufferedWriter.write(int c)方法範例

2019-10-16 22:17:43

Java BufferedWriter.write(int c)方法範例

BufferedWriterJava BufferedWriter.write(int c)方法的語法如下。

public void write(int c) throws IOException

範例

在下面的程式碼中展示了如何使用BufferedWriter.write(int c)方法。

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

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

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    for (int i = 65; i <= 90; i++) {
      bw.write(i);
    }

    bw.flush();

    StringBuffer sb = sw.getBuffer();

    System.out.println(sb);

  }
}

上面的程式碼生成以下結果。

ABCDEFGHIJKLMNOPQRSTUVWXYZ